**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Compare 2 arrays using for loop and if statement

31 views (last 30 days)

Show older comments

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 Comments

Rik
on 17 Oct 2020

You forgot an opening parenthesis in your if, just like you forgot to format your code as code.

Bruno Luong
on 17 Oct 2020

If you replace

M : user defined

by something MATLAB can understand and populate correctly M, your code will run

Dua Fatima
on 17 Oct 2020

Edited: Dua Fatima
on 17 Oct 2020

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
on 17 Oct 2020

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
on 17 Oct 2020

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
on 17 Oct 2020

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
on 17 Oct 2020

Edited: Dua Fatima
on 17 Oct 2020

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
on 17 Oct 2020

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
on 17 Oct 2020

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

### Answers (1)

Asad (Mehrzad) Khoddam
on 18 Oct 2020

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

##### 0 Comments

### See Also

### Categories

### Tags

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)