views:

4027

answers:

2

Hi,

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.

+1  A: 

Since this is obviously homework, I won't give you the complete answer.

There are several ways to index into a matrix. When you have a scattered index set as this is, you need to use a single index. Thus if

A = rand(5,6)
A =
      0.81472      0.09754      0.15761      0.14189      0.65574      0.75774
      0.90579       0.2785      0.97059      0.42176     0.035712      0.74313
      0.12699      0.54688      0.95717      0.91574      0.84913      0.39223
      0.91338      0.95751      0.48538      0.79221      0.93399      0.65548
      0.63236      0.96489      0.80028      0.95949      0.67874      0.17119

A(3:4,3:4)

will yield a 2x2 submatrix from A. But we can also find that submatrix as

reshape(A([13 14 18 19]),[2 2])
ans =
      0.95717      0.91574
      0.48538      0.79221

Why did I choose this index set? For the answer, you will need to read about sub2ind.

[I,J] = ndgrid(3:4,3:4);
sub2ind([5 6],I(:),J(:))
ans =
    13
    14
    18
    19

In the end, it looks like you want a 15x100x100 array from the extracted parts. So build the necessary index array up from the pieces I've shown. You will need to do a final reshape at the end to make it the proper shape.

This should give you enough of a start to finish your homework.

woodchips
+3  A: 

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{:});

EXPLANATION:

The first line creates an anonymous function. This is a simple one line function that can be created on-the-fly without having to put it in an m-file. The function defines two inputs r and c which are used to extract a 100-by-100 submatrix from cam1. The variable indexFcn stores a function handle which is used to call the function. Note that the values of cam1 used by the anonymous function are static. Even if the values in the variable cam1 change, the anonymous function still uses the values that were in cam1 when the function was created.

The second line makes a call to ARRAYFUN, which applies a function to every element of an array. ARRAYFUN loops over each entry in x and y, passing the values to indexFcn. The output is stored in result, a 15-element cell array where each cell contains a 100-by-100 matrix.

The third line uses the CAT function to concatenate the 100-by-100 matrices into a 100-by-100-by-15 matrix.

gnovice
after having posted the question i did some more research and found the arrayfun function and i came up with the following:digval1 = arrayfun(@(i) sum(sum(cam1(y(i):(y(i)+99), x(i):(x(i)+99)))) / 10000, 1:15);this includes already the next step i had to do: calculate the average over the parts. but thanks a lot for your help, i know now that i did the right thing and i have used your code to get a more beautiful line:digval1 = arrayfun(@(i, j) sum(sum(cam1(j:j+99, i:i+99))) / 10000, x, y);
srcHG
Glad to help! FYI: If you have access to the Image Processing Toolbox, there is a function called MEAN2 (http://www.mathworks.com/access/helpdesk/help/toolbox/images/mean2.html) that would simplify your function: digval1 = arrayfun(@(i,j) mean2(cam1(j:j+99,i:i+99)),x,y); Alternatively, you can write your own MEAN2 function: mean2 = @(A) mean(A(:));
gnovice
now that's sexy :)
srcHG

related questions