Compare 2 arrays using for loop and if statement

I want to compare enteries of 2 arrays and if they follow the condition, store the value at the same index in a separate array.
The following code is giving me an error. Please let me know what else can I use?
I want to return the 2D array M_filter_1
M =[1 2 3 ; 8 9 0];
e = 2;
M_filter_1=[[]];
n = size(M,2)
m = size(M,1)
A =[1 2 3 4 5 6 7 8 9 0];
for k = 1:1:size(A,2)
for i= 1:1:m
for j = 1:1:n
if (M(i,j)> A(k) - e) & (M (i,j)< A(k)+ e)
M_filter_1(i,j) = M(i,j);
else
M_filter_1(i,j)= 0 ;
end
end
end
end

16 件のコメント

Rik
Rik 2020 年 10 月 17 日
You forgot an opening parenthesis in your if, just like you forgot to format your code as code.
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
Yes might be when I copy pasted it but its still not working :)
Bruno Luong
Bruno Luong 2020 年 10 月 17 日
If you replace
M : user defined
by something MATLAB can understand and populate correctly M, your code will run
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
Actually I did, but its still giving me an error in the if condition.
Rik
Rik 2020 年 10 月 17 日
What error did you get? I would also suggest changing it to abs(a-b)<e.
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
編集済み: Dua Fatima 2020 年 10 月 17 日
Thanks, I did, but its still giving me the error:
Error in test4 (line 10)
if abs(M(i,j)- A(k))< e
Walter Roberson
Walter Roberson 2020 年 10 月 17 日
if (M(i,j)> A(k) + e) & (M (i,j)< A(k)+ e)
So the M entry has to be strictly greater than A(k)+e, but it also has to be strictly less than A(k)+e .
There are no numbers that are both strictly greater than and strictly less than the same quantity.
The only way that the test could be satisfied, in theory, is if you rewrote it to
if ~(M(i,j) <= A(k) + e) & ~(M (i,j)>= A(k)+ e)
If you stick to real numbers, then P>Q is equivalent to ~(P<=Q) and so you would normally expect the two if statements to be the same. But double precision arithmetic allows for one quantity that is not a number, and which has strange comparison behaviours. The quantity that is not a number is known as NaN -- literally Not A Number. And for NaN, P>Q and P<=Q are both false -- but ~false is true, so ~(P<=Q) is true if P>Q or if one of the values is NaN, and likewise ~(P>=Q) is true if P<Q or one of the values is nan; when you & those two tests together, the combination cannot be satisfied if P and Q are finite or inf or +inf, but the combination can be satisfied of one of the quantities is NaN.
Testing for and of a condition and its opposite, has been used in real production code historically in order to detect NaN, in languages in which there is no isnan() test, so the kind of code you wrote is not entirely useless. It is just not appropriate for the circumstances, and would have to be changes along the lines I show if your intention was to test for NaN.
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
Thankyou so much for your comment! It really clarified things for me.
My goal is to find out entries in M that match the criteria given and store them in M_filter_1 at the same index. I do understand that none of the enteries satisfy the criteria but M here is just dummy and in actual I'll be asking for a user input. The issue here is the error I am getting an error in the if statement and not the answer to this ( as I am ignoring it for the moment).
Walter Roberson
Walter Roberson 2020 年 10 月 17 日
M =[1 2 3 ; 8 9 0];
e = 2;
M_filter_1=[[]];
n = size(M,2)
n = 3
m = size(M,1)
m = 2
A =[1 2 3 4 5 6 7 8 9 0];
for k = 1:1:size(A,2)
for i= 1:1:m
for j = 1:1:n
if (M(i,j)> A(k) + e) & (M (i,j)< A(k)+ e)
M_filter_1(i,j) = M(i,j);
else
M_filter_1(i,j)= 0 ;
end
end
end
end
M_filter_1
M_filter_1 = 2×3
0 0 0 0 0 0
As you can see, there is no error message in the if statement. And I explained the reason it comes out all zero: because it is impossible to satisfy that if condition because you require that M(i,j) be simultaneously greater than and less than the same number A(k)+e .
What through the code mentally. When i, j, k are all 1, you are comparing M(1,1)>1+e & M(1,1)<1+e . What value would M(1,1) have to have to satisfy that test?
I wonder if you are wanting M(i,j)>A(k)-e & M(i,j) < A(k+1)+e ?
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
編集済み: Dua Fatima 2020 年 10 月 17 日
M(i,j)>A(k)-e & M(i,j) < A(k)+e
Yes, thats what I aim to do ( Must have made a typo here).
I am getting all zeroes even if there is a match.
Walter Roberson
Walter Roberson 2020 年 10 月 17 日
M =[1 2 3 ; 8 9 0];
e = 2;
M_filter_1=[[]];
n = size(M,2)
n = 3
m = size(M,1)
m = 2
A =[1 2 3 4 5 6 7 8 9 0];
for k = 1:1:size(A,2)
for i= 1:1:m
for j = 1:1:n
if (M(i,j)> A(k) - e) & (M (i,j)< A(k)+ e)
M_filter_1(i,j) = M(i,j);
else
M_filter_1(i,j)= 0 ;
end
end
end
end
M_filter_1
M_filter_1 = 2×3
1 0 0 0 0 0
Not all zero -- the first entry is non-zero.
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
Yes. Thankyou so much!
It's still returning all zeroes on my system though. I think there might be some issue with my system then.
Walter Roberson
Walter Roberson 2020 年 10 月 17 日
Which release are you using?
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
I am using 2020b.
Walter Roberson
Walter Roberson 2020 年 10 月 18 日
Dua Fatima
Dua Fatima 2020 年 10 月 18 日
Aright. Thankyou so much!

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

回答 (1 件)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 10 月 18 日

1 投票

M =[1 2 3 ; 8 9 0];
e = 2;
m = size(M,1);
n = size(M,2);
M_filter_1 = zeros(size(M));
A =[1 2 3 4 5 6 7 8 9 0];
for a = A
for i= 1:1:m
for j = 1:1:n
if (M(i,j)> a - e) && (M (i,j)< a + e)
M_filter_1(i,j) = M(i,j);
end
end
end
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2020 年 10 月 17 日

コメント済み:

2020 年 10 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by