필터 지우기
필터 지우기

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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by