Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

comparing relevant elemnts of two matrix

Asked by som

som (view profile)

on 30 Nov 2012

Hi all,

I have two matrics like below:

      a=[9 7 NaN; 6 3	8; 15 NaN 5; NaN 4 2];
      b= [10,14,NaN;10,10,13;10,NaN,10;NaN,10,10;] ;

I want to see if each element of "a" is less than or equal to its corresponding element in "b"?

 how can I write this program .
 Thanks in advance. 

1 Comment

Can you mention the range?

som

som (view profile)

Tags

Products

No products are associated with this question.

4 Answers

Answer by Image Analyst

Image Analyst (view profile)

on 30 Nov 2012
Accepted answer

Maybe this is what you want:

a=[9 7 NaN; 6 3	8; 15 NaN 5; NaN 4 2]
b= [10,14,NaN;10,10,13;10,NaN,10;NaN,10,10;]
% Subtract the two. 
% The value will be negative when a(r,c) < b(r,c).
differenceMatrix = a-b
% Make that into a boolean map of where a<b.
% It will be true (1) where a<b.
mapOfWhereAisSmallerThanB = differenceMatrix<0
% Just for fun, do the converse.
% Make that into a boolean map of where b<a.
% It will be true (1) where b<a.
mapOfWhereBisSmallerThanA = differenceMatrix > 0
% See if all non-Nan values have a<b
nonNanIndexes = ~isnan(a) & ~isnan(b)
if all(differenceMatrix(nonNanIndexes) < 0)
	msgbox('All of a < b');
else
	msgbox('Not all of a is < b');
end

In the command window:

a =
     9     7   NaN
     6     3     8
    15   NaN     5
   NaN     4     2
b =
    10    14   NaN
    10    10    13
    10   NaN    10
   NaN    10    10
differenceMatrix =
    -1    -7   NaN
    -4    -7    -5
     5   NaN    -5
   NaN    -6    -8
mapOfWhereAisSmallerThanB =
     1     1     0
     1     1     1
     0     0     1
     0     1     1
mapOfWhereBisSmallerThanA =
     0     0     0
     0     0     0
     1     0     0
     0     0     0
nonNanIndexes =
     1     1     0
     1     1     1
     1     0     1
     0     1     1

0 Comments

Image Analyst

Image Analyst (view profile)

Answer by Harshit

Harshit (view profile)

on 30 Nov 2012

size((A-B)>0)

0 Comments

Harshit

Harshit (view profile)

Answer by Vishal Rane

Vishal Rane (view profile)

on 30 Nov 2012
Edited by Vishal Rane

Vishal Rane (view profile)

on 30 Nov 2012

By 'little-equal' I assume you mean 'less than or equal'.

Use

 a <= b

assuming a,b are of same dimensions.

See Relational Operators for more info.

0 Comments

Vishal Rane

Vishal Rane (view profile)

Answer by Wayne King

Wayne King (view profile)

on 30 Nov 2012
Edited by Wayne King

Wayne King (view profile)

on 30 Nov 2012

I'll assume that " littel-equal " means "less than or equal to"

 a=[9 7 NaN; 6 3	8; 15 NaN 5; NaN 4 2];
 b= [10,14,NaN;10,10,13;10,NaN,10;NaN,10,10;] ;
 indices = find(a<=b);
 a(indices)

The above gives the elements of a that are less than or equal to the corresponding element of b.

or

C = a<=b;

The matrix C has a 1 where the element of a is less than or equal to the element of b and a 0 otherwise

1 Comment

som

som (view profile)

on 30 Nov 2012

Thanks for your answer, but please pay attention that both matrics have "NaN" elements. so the problem is still remaind.

Any help would be appreciated.

Wayne King

Wayne King (view profile)

Contact us