How to store results from every loop in For-Loop

1 回表示 (過去 30 日間)
Meshooo
Meshooo 2012 年 12 月 20 日
Hello everyone; I want to load the whole 21 intensity images (Intensity) from the folder and find their intensity value in 6 points which I already know them.
I wrote the following program which works very well but I want to use a for loop because the number of points is not always 6.
Could you please check this code to make a for loop
for k = 1:(numel(filelist));
Intensity = imread(filelist(k).name);
P_Centro_1 = []; %1st centroid
P_Centro_1 = impixel(Intensity,(center(1,1)),(center(1,2)));
P_Centro_1 = P_Centro_1(:,1); %Intensity value
A1(:,k)= P_Centro_1;
P_Centro_2 = []; %2nd centroid
P_Centro_2 = impixel(Intensity,(center(2,1)),(center(2,2)));
P_Centro_2 = P_Centro_2(:,1);
A2(:,k)= P_Centro_2;
P_Centro_3 = []; %3rd centroid
P_Centro_3 = impixel(Intensity,(center(3,1)),(center(3,2)));
P_Centro_3 = P_Centro_3(:,1);
A3(:,k)= P_Centro_3;
P_Centro_4 = []; %4th centroid
P_Centro_4 = impixel(Intensity,(center(4,1)),(center(4,2)));
P_Centro_4 = P_Centro_4(:,1);
A4(:,k)= P_Centro_4;
P_Centro_5 = []; %5th centroid
P_Centro_5 = impixel(Intensity,(center(5,1)),(center(5,2)));
P_Centro_5 = P_Centro_5(:,1);
A5(:,k)= P_Centro_5;
P_Centro_6 = []; %6th centroid
P_Centro_6 = impixel(Intensity,(center(6,1)),(center(6,2)));
P_Centro_6 = P_Centro_6(:,1);
A6(:,k)= P_Centro_6;
end
hold on
plot(A1,'b-')
plot(A2,'c-')
plot(A3,'g-')
plot(A4,'y-')
plot(A5,'r-')
plot(A6,'m-')
hold off
So the following could be the answer but how to store the results from every for loop before going to the 2nd loop?
for k = 1:21
Intensity = imread(filelist(k).name);
for c = 1:6
P_Centro_1 = []; % list of centroids
P_Centro_1 = impixel(Intensity,(center(c,1)),(center(c,2)));
P_Centro_1 = P_Centro_1(:,1); %Intensity value
A1(:,k)= P_Centro_1;
end
end
  1 件のコメント
Jonathan Epperl
Jonathan Epperl 2012 年 12 月 20 日
You will probably want to use a cell array, whose elements can be pretty much everything:
C = cell(2,3);
C{1,1} = 'string';
C{1,2} = 3.1415;
C{2,1} = zeros(10,10);
C{2,3} = eye(3);

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 20 日
for k = 1:21
Intensity = imread(filelist(k).name);
for c = 1:6
P_Centro_1 = impixel(Intensity,(center(c,1)),(center(c,2)));
A1{k,c} = P_Centro_1(:,1); %Intensity value
end
end
  2 件のコメント
Meshooo
Meshooo 2012 年 12 月 21 日
編集済み: Meshooo 2012 年 12 月 21 日
Yes, it works. Thank you very much. But I think we should use () not {}
A1(k,c) = P_Centro_1(:,1); %Intensity value
Meshooo
Meshooo 2012 年 12 月 26 日
Hi Azziz,
I am still having small programming problem which I also post it as a question to every one.
From my previous problem, I will have an array 'A1' that has 21 rows and 6 columns. I want to find the Extended-maxima of each column using the matlab imextendedmax function.
I don't know what is wrong with my code!
for x = 1:6
A1_Colm = imextendedmax(A1(:,x), 1) %Extended-maxima with 1 H-maxima transform
M (21,6) = A1_Colm1; % to save the results from each loop in a new array M
end
The following error appears Subscripted assignment dimension mismatch.
Do you have any idea!
Thank you for your time..

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by