フィルターのクリア

How to inherit values from for loop into if else loop?

2 ビュー (過去 30 日間)
Tinna Armasamy
Tinna Armasamy 2017 年 4 月 25 日
編集済み: Walter Roberson 2017 年 4 月 26 日
This is my code :
for i=1:n
Area(i)=k(i).Area;
Diameter(i)=sqrt(4*[k(i).Area]/pi);
MajorAxis(i)=k(i).MajorAxisLength;
end
Now I want to use the MajorAxis value into if else loop. Eg :-
if MajorAxis(i) < 0.002
Clay(i)=MajorAxis(i);
end
But the (i) values from for loop are not being inherited.
  3 件のコメント
Tinna Armasamy
Tinna Armasamy 2017 年 4 月 25 日
編集済み: Walter Roberson 2017 年 4 月 26 日
i mean that the i values are not performing the if else loop
Jan
Jan 2017 年 4 月 25 日
This is unclear: "i values not performing the if else loop". I cannot imagine, what this should mean. There is neither an "if else loop", nor do loop counters "perform" anything - most of all not outside the loop.

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

回答 (1 件)

Jan
Jan 2017 年 4 月 25 日
There is no magic "inheriting" of loop counters. I think the main problem is hiddon in your formulation "if else loop": The if command is not a loop.
You have to include the if into the loop:
Area = zeros(1, n); % Pre-allocate!
Diameter = zeros(1, n);
MajorAxis = zeros(1, n);
Clay = zeros(1, n);
for i=1:n
Area(i) = k(i).Area;
Diameter(i) = sqrt(4*[k(i).Area]/pi);
MajorAxis(i) = k(i).MajorAxisLength;
if MajorAxis(i) < 0.002
Clay(i) = MajorAxis(i);
end
end
Or you can omit the loop:
Area = [k.Area];
Diameter = sqrt(4 * Area / pi);
MajorAxis = [k.MajorAxisLength];
Clay = zeros(1, n);
Index = (MajorAxis < 0.002);
Clay(Index) = MajorAxis(Index);
  1 件のコメント
Tinna Armasamy
Tinna Armasamy 2017 年 4 月 26 日
編集済み: Walter Roberson 2017 年 4 月 26 日
soilgray=getimage(handles.axes1);
soilgray=rgb2gray(soilgray);
level=graythresh(soilgray);
im=im2bw(soilgray,level);
axes(handles.axes2);
imshow(im);
cc = bwconncomp(im,8);
n= cc.NumObjects;
set(handles.text11,'String',n);
Area = zeros(n,1);
Diameter = zeros(n,1);
MajorAxis = zeros(n,1);
Clay=zeros(1,n);
Silt=zeros(1,n);
Sand=zeros(1,n);
k = regionprops(cc,'Area','EquivDiameter','MajorAxisLength');
for i=1:n
Area(i) = k(i).Area;
Diameter(i) = sqrt(4*[k(i).Area]/pi);
MajorAxis(i) = k(i).MajorAxisLength;
Diameter(i) = (Diameter(i)*25.4)/300;
if Diameter(i) < 0.002
Diameter(i)=Clay(i);
else if Diameter(i) >=0.002 && Diameter(i) < 0.05
Diameter(i)=Silt(i);
else if Diameter(i) >= 0.05 && Diameter(i) < 2
Diameter(i)=Sand(i);
end
end
end
end
NumC = numel(Clay);
ClayPercentage = (NumC/n)*100;
set(handles.text15,'String',ClayPercentage);
NumC = numel(Silt);
SiltPercentage = (NumC/n)*100;
set(handles.text16,'String',SiltPercentage);
NumC = numel(Sand);
SandPercentage = (NumC/n)*100;
set(handles.text17,'String',SandPercentage);
This is what I have done so far. The numel value of Silt, Clay and Sand shows the n value and not the number after performing if else.

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by