If statement not giving the desired output

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

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

Its still returning all zeroes
Have you exactly copied and pasted the code from here? Can you show the code you are running?
It's running alright but in my case, M comes from an absolute value after subtraction:
for i=1:1:n
for j = 1:1:n
M(i,j)= abs(A(i)-A(j));
end
end
%A is an array storing user defined values which are converted to double by str2double
What are the values in A? I mean in which range? If this condition
M(i,j)< 157.02 + e) && (M(i,j)>157.02- e
is not satisfied, you will get all zeros.
It's working now!
Thanks alot!
I am glad to be of help!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!