Compare 2 arrays using for loop and if statement

10 ビュー (過去 30 日間)
Dua Fatima
Dua Fatima 2020 年 10 月 17 日
コメント済み: Dua Fatima 2020 年 10 月 18 日
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 件のコメント
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 日
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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by