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
2020 年 10 月 17 日
You forgot an opening parenthesis in your if, just like you forgot to format your code as code.
Dua Fatima
2020 年 10 月 17 日
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
2020 年 10 月 17 日
Rik
2020 年 10 月 17 日
What error did you get? I would also suggest changing it to abs(a-b)<e.
Dua Fatima
2020 年 10 月 17 日
編集済み: Dua Fatima
2020 年 10 月 17 日
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
2020 年 10 月 17 日
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
M_filter_1
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
2020 年 10 月 17 日
編集済み: Dua Fatima
2020 年 10 月 17 日
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
M_filter_1
Not all zero -- the first entry is non-zero.
Dua Fatima
2020 年 10 月 17 日
Walter Roberson
2020 年 10 月 17 日
Which release are you using?
Dua Fatima
2020 年 10 月 17 日
Walter Roberson
2020 年 10 月 18 日
The output I showed in https://www.mathworks.com/matlabcentral/answers/616693-compare-2-arrays-using-for-loop-and-if-statement#comment_1066638 with the [1 0 0; 0 0 0] output, was run on R2020b.
Dua Fatima
2020 年 10 月 18 日
回答 (1 件)
Asad (Mehrzad) Khoddam
2020 年 10 月 18 日
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 Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!