How to MATLAB: Extract multiple parts of a matrix without using loops

 I have a huge 2D matrix and I would like to extract 15 different 100x100 parts out of it. I have two vectors x and y where the top left indices of the parts are saved. I have used something like this: 

 

 result = cam1(x(1:end):(x(1:end)+99), y(1:end):(y(1:end)+99));

but the result is just a 100x100 matrix instead of a 15x100x100. Why?

I know it could easily be done using a loop but we are not allowed to use loops (it's part of an image processing exercise). Another possbility would be to write all the 15 lines but that is kind of ugly.

Do you have any elegant solution? Thanks.  

NOTE:-


Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.   

Answers: 

There are a number of ways you could do this without loops. Most solutions involve expanding the vectors x and y into larger matrices of indices and would likely use one or more of the functions REPMAT, BSXFUN, or SUB2IND. A good tutorial for matrix indexing can be found here.

However, since you asked for an elegant solution, here's one that's somewhat unusual. It uses anonymous functions as well as the functions ARRAYFUN and CAT: 

 

indexFcn = @(r,c) cam1(r:(r+99),c:(c+99));
result = arrayfun(indexFcn,x,y,'UniformOutput',false);
result = cat(3,result{:}); 
SEE COMPLETE ANSWER CLICK THE LINK  
https://matlabhelpers.com/questions/how-to-matlab-extract-multiple-parts-of-a-matrix-without-using-loops.php

Comments

Popular posts from this blog

programmingshark

Why do I get a "Too many input arguments" error when not passing any?

Constructing piecewise symbolic function in Matlab