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

Thread Subject:
comparision inside matrix

Subject: comparision inside matrix

From: Erik L

Date: 14 Jun, 2010 10:18:04

Message: 1 of 4

I have a matrix A=[0 2 3 4 3 1 0]

I want to check each element of A in if condition:

if ANY element is greater than or equal to 2, my condition is false.

I don't want to use the following code:

If(A(1,1)<2 && A(1,2)<2 && A(1,3)<2 && A(1,4)<2.........

.....


Is there any alternative, because i have a very huge row vector.

thanks

Erik

Subject: comparision inside matrix

From: Wayne King

Date: 14 Jun, 2010 10:28:11

Message: 2 of 4

"Erik L" <lookgrn@gmail.com> wrote in message <hv4vks$ogs$1@fred.mathworks.com>...
> I have a matrix A=[0 2 3 4 3 1 0]
>
> I want to check each element of A in if condition:
>
> if ANY element is greater than or equal to 2, my condition is false.
>
> I don't want to use the following code:
>
> If(A(1,1)<2 && A(1,2)<2 && A(1,3)<2 && A(1,4)<2.........
>
> .....
>
>
> Is there any alternative, because i have a very huge row vector.
>
> thanks
>
> Erik

Hi Erik,

A = [0 2 3 4 3 1 0];
if (any(A>2))
disp('There is an element greater than 2.');
else
disp('No element is greater than 2');
end


Wayne

Subject: comparision inside matrix

From: Erik L

Date: 14 Jun, 2010 10:37:05

Message: 3 of 4

"Wayne King" <wmkingty@gmail.com> wrote in message <hv507r$1pa$1@fred.mathworks.com>...
> "Erik L" <lookgrn@gmail.com> wrote in message <hv4vks$ogs$1@fred.mathworks.com>...
> > I have a matrix A=[0 2 3 4 3 1 0]
> >
> > I want to check each element of A in if condition:
> >
> > if ANY element is greater than or equal to 2, my condition is false.
> >
> > I don't want to use the following code:
> >
> > If(A(1,1)<2 && A(1,2)<2 && A(1,3)<2 && A(1,4)<2.........
> >
> > .....
> >
> >
> > Is there any alternative, because i have a very huge row vector.
> >
> > thanks
> >
> > Erik
>
> Hi Erik,
>
> A = [0 2 3 4 3 1 0];
> if (any(A>2))
> disp('There is an element greater than 2.');
> else
> disp('No element is greater than 2');
> end
>
>
> Wayne

Thanks Wayne

If my condition is if all elements are <2, what will be the exact code.

Cheers
Erik

Subject: comparision inside matrix

From: Erik L

Date: 14 Jun, 2010 10:39:04

Message: 4 of 4

"Wayne King" <wmkingty@gmail.com> wrote in message <hv507r$1pa$1@fred.mathworks.com>...
> "Erik L" <lookgrn@gmail.com> wrote in message <hv4vks$ogs$1@fred.mathworks.com>...
> > I have a matrix A=[0 2 3 4 3 1 0]
> >
> > I want to check each element of A in if condition:
> >
> > if ANY element is greater than or equal to 2, my condition is false.
> >
> > I don't want to use the following code:
> >
> > If(A(1,1)<2 && A(1,2)<2 && A(1,3)<2 && A(1,4)<2.........
> >
> > .....
> >
> >
> > Is there any alternative, because i have a very huge row vector.
> >
> > thanks
> >
> > Erik
>
> Hi Erik,
>
> A = [0 2 3 4 3 1 0];
> if (any(A>2))
> disp('There is an element greater than 2.');
> else
> disp('No element is greater than 2');
> end
>
>
> Wayne

if(all(A<2)

thanks Wayne, i got it.

cheers

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us