save while runing loop
1 view (last 30 days)
Show older comments
André Sousa
on 26 May 2014
Commented: bashar halleem
on 20 May 2020
This is my full code:
aa=rand(2,2,2)
aa(1,1,1)=5;
aa(2,1,1)=8;
aa(1,2,1)=3;
aa(2,2,1)=3;
aa(1,1,2)=7;
aa(2,1,2)=8; aa(1,2,2)=3;
aa(2,2,2)=3;
dimention_aa=size(aa); aareshaped=reshape(aa,[2,4])
ii=rand(3,1);
ii(1,1)=8;
ii(2,1)=3;
ii(3,1)=7;
for( l=(ii(1):ii(end)))
ind_reshaped=find(aareshaped==l)
[R1, C1, S1]=ind2sub(dimention_aa,find(aareshaped==l)
k=[R1, C1, S1]
x1=k(:,1)
y1=k(:,2)
z1=k(:,3)
newMatrix = zeros(dimention_aa)
for j = 1 : size(k, 1)
newMatrix( x1(j) , y1(j) ,z1(j)) = 1
end
% roi_mat(l,:)=x;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%Basically the k matrix(that has lines correspondent to xyz positions) is genereated by
[R1, C1, S1]=ind2sub(dimention_aa,find(aareshaped==l)
k=[R1, C1, S1]
And l is a vector with diferent numbers. How do I do the same procedure to go through every element of "l"; and then save the "newMatrix with the corresponding index? The code I wrote above, doesn't work quite well.. Regards
1 Comment
Image Analyst
on 26 May 2014
And this is the tutorial: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Accepted Answer
Geoff Hayes
on 26 May 2014
Hi André - a couple of things: the above doesn't run because of the missing bracket at the end of the second line in
ind_reshaped=find(aareshaped==l)
[R1, C1, S1]=ind2sub(dimention_aa,find(aareshaped==l)
Since the first line above is unused, just remove it and add the closing bracket (and semi-colon) to the end of the second line
[R1, C1, S1]=ind2sub(dimention_aa,find(aareshaped==l));
The code is iterating over the contents of the ii vector but the body of the for loop never gets evaluated because the code iterates as
for( l=(ii(1):ii(end)))
which fails on the first attempt since the first element (of ii) is 8 and the second element is 3: 8>3 so the for loop ends before it begins. Instead, the code could iterate over the contents of the ii vector and just assign l as the first line in the body of the for loop
for a=1:length(ii)
l = ii(a);
% etc.
Now you want to save the newMatrix at each iteration of the outer for loop. There are several ways you can do this, but since you want to save the index as well, the easiest way is to just use a cell array
data = {}; % this array will contain the matrix and index pairs
atEl = 1; % this is the next index into the above array
for a=1:length(ii)
l = ii(a);
% etc.
Then after the inner for loop has been completed, just save newMatrix and the index (is this k or l?) as
for j = 1 : size(k, 1)
newMatrix( x1(j) , y1(j) ,z1(j)) = 1
end
data{atEl}.mtx = newMatrix;
data(atEl}.idx = k; % or l or ??
atEl = atEl + 1; % increment to the next index
You may also want to consider adding semi-colons to the end of each assignment statement in your code so that the result of that is not written to the console on each iteration.
You may also want to add some error checking code to handle the case where find(aareshaped==l) returns an empty matrix.
Hope that the above helps!
3 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!