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:
matrix element setting

Subject: matrix element setting

From: mat001

Date: 13 Aug, 2010 11:08:05

Message: 1 of 4

I have a 2d matrix and i want to set something like that

if any element of matrix will be greater than maximum value (let say 50) then error

how to do that

Please reply me soon.

Regards

Subject: matrix element setting

From: Sean

Date: 13 Aug, 2010 12:40:24

Message: 2 of 4

"mat001 " <priya.biomath@yahoo.co.in> wrote in message <i4392l$2is$1@fred.mathworks.com>...
> I have a 2d matrix and i want to set something like that
>
> if any element of matrix will be greater than maximum value (let say 50) then error
>
> how to do that
>
> Please reply me soon.
>
> Regards

if(any(matrix>50))
  error('Ha!');
end

Subject: matrix element setting

From: Walter Roberson

Date: 13 Aug, 2010 13:21:56

Message: 3 of 4

Sean wrote:
> "mat001 " <priya.biomath@yahoo.co.in> wrote in message
> <i4392l$2is$1@fred.mathworks.com>...
>> I have a 2d matrix and i want to set something like that
>>
>> if any element of matrix will be greater than maximum value (let say
>> 50) then error
>>
>> how to do that
>>
>> Please reply me soon.
>>
>> Regards
>
> if(any(matrix>50))
> error('Ha!');
> end

Sean, the original poster indicated that it was a 2D matrix.
matrix > 50 would result in a 2D matrix of logical values
any() applied to that would result in a row vector of logical values
"if" applied to that would be the same as "if all(...)" and so would
test if *all* of the columns had at least one value greater than 50.

The easiest fix to this is

if any(matrix(:)>50))

Subject: matrix element setting

From: Sean

Date: 13 Aug, 2010 13:57:04

Message: 4 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <UFb9o.59107$dx7.43323@newsfe21.iad>...
> Sean wrote:
> > "mat001 " <priya.biomath@yahoo.co.in> wrote in message
> > <i4392l$2is$1@fred.mathworks.com>...
> >> I have a 2d matrix and i want to set something like that
> >>
> >> if any element of matrix will be greater than maximum value (let say
> >> 50) then error
> >>
> >> how to do that
> >>
> >> Please reply me soon.
> >>
> >> Regards
> >
> > if(any(matrix>50))
> > error('Ha!');
> > end
>
> Sean, the original poster indicated that it was a 2D matrix.
> matrix > 50 would result in a 2D matrix of logical values
> any() applied to that would result in a row vector of logical values
> "if" applied to that would be the same as "if all(...)" and so would
> test if *all* of the columns had at least one value greater than 50.
>
> The easiest fix to this is
>
> if any(matrix(:)>50))

That is correct, sorry.

Tags for this Thread

No tags are associated with 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