If statement not giving the desired output

조회 수: 1 (최근 30일)
Dua Fatima
Dua Fatima 2020년 10월 18일
댓글: Ameer Hamza 2020년 10월 18일
I am using a simple if statement but it is not returning the correct result even when the condition is satisfied. No errors are showing.
e and M are user defined and converted from string to double using str2double on input!
M_1=[[]];
for i=1:1:size(M,1)
for j = 1:1:size(M,2)
if (M(i,j)< 157.02 + e)& (M(i,j)>157.02- e)
M_1 (i,j) = "A";
else
M_1(i,j) = "0";
end
end
end

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 18일
Writing
M_1=[[]]
define it as an empty double array. Try following code
M = 200*rand(5); % example values
e = 5;
M_1=strings(size(M));
for i=1:1:size(M,1)
for j = 1:1:size(M,2)
if (M(i,j)< 157.02 + e) && (M(i,j)>157.02- e)
M_1 (i,j) = "A";
else
M_1(i,j) = "0";
end
end
end
  댓글 수: 6
Dua Fatima
Dua Fatima 2020년 10월 18일
It's working now!
Thanks alot!
Ameer Hamza
Ameer Hamza 2020년 10월 18일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by