If statement not giving the desired output

1 view (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 18 Oct 2020
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!