save while runing loop

조회 수: 1 (최근 30일)
André Sousa
André Sousa 2014년 5월 26일
댓글: bashar halleem 2020년 5월 20일
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

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 5월 26일
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
Geoff Hayes
Geoff Hayes 2014년 5월 27일
My pleasure!
bashar halleem
bashar halleem 2020년 5월 20일
Realy it is good Answer

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by