Accesing data in a three dimensional matrix using a for loop

1 vue (au cours des 30 derniers jours)
Martha
Martha le 30 Août 2014
Commenté : Martha le 30 Août 2014
Hi, I want to create a for loop to go through each of the pages (20) of a matrix 400x600x20 (called polygons). The matrices 400x600 are binary matrices with ones(1) and zeros(0), with different polygons in each of the 20 pages. I have an original matrix (called main01) of 400x600 from where I want to use the data to copy the ones(1) and zeros(0) and locate it in the polygons in each of the 20 pages of the other variable. So I try this creating a variable of only the first page of the matrix polygons:
polygon1(polygon1==1)=main01(polygon1==1);
And it works very well. However, when I set this into a for loop it doesn't give me the zeros and ones of the main01 matrix, it just return the same matrix as polygons.
count=20;
for u=1:1:count;
if polygons(:,:,u)==1
polygons=main01(polygons(:,:,u)==1;
end
end
I will appreciate some help with this for loop.
Thank you. Martha

Réponse acceptée

Guillaume
Guillaume le 30 Août 2014
Modifié(e) : Guillaume le 30 Août 2014
First off, the big error in your code is:
if polygons(:,:,u)==1
This will only be true when polygons(:,:,u) is all ones. Furthermore, I'm not sure what you were trying to achieve with this if.
A simple way to do what you want would be to use a temporary variable to store the page you're working on:
count = 20;
for u=1:count %don't need semicolon at the end. don't need increment when it is 1.
polygon = polygons(:,:,u); %temporary 2d variable
polygon(polygon == 1) = main01(polygon == 1); %as you wrote
polygons(:,:,u) = polygon; %update page with temporary
end
But actually, the operation you're doing is a simple logical AND, thus you can simplify the code with:
for u=1:count
polygons(:,:,u) = polygons(:,:, u) & main01;
end
If the operation you're trying to perform between polygons and main01 are actually more complex, I would change a few things. First, I would declare polygons and main01 as logical. That way you don't have to do the == 1 anymore.
Secondly, instead of storing the polygons as a 3d matrix, I would store them as a cell array of 2d matrix. If I rewrite my initial example that way:
count = 20;
%first two lines just convert existing variables. Better to declare them
%that way in the first place
polygons = arrayfun(@(u) logical(polygons(:,:,u), 1:count, 'uni', false); %polygons is a cell array of 2d logical matrices
main01 = logical(main01);
for u=1:count
polygons{u}(polygons{u}) = main01(polygons{u});
%or:
%polygons{u} = polygons{u} & main01;
end
  2 commentaires
Guillaume
Guillaume le 30 Août 2014
Also, just realised you can also avoid the loop altogether,
for u=1:count
polygons(:,:,u) = polygons(:,:, u) & main01;
end
is equivalent to:
polygons = bsxfun(@and, polygons, main01);
Martha
Martha le 30 Août 2014
Thank you very much Guillaume, I tried all the three ways and it works very well.. I will give it a go creating a cell array as well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by