Quantcast

Vectorization with max / min in matrix

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Vectorization with max / min in matrix

Pedro-2
Hi,

I have two vectors/matrix, and I want to get his minimum values for
every position without doing for/while/etc statement

But there is a problem:

a = randi([1 31],1,10);
b = randi([1 31],1,10);
a( a < b ) = b;
error: A(I) = X: X must have the same size as I

I don't know why doesn't work, have the same size (perhaps it is not a
bug because is the same error in Matlab).
What is going wrong?

The original idea was to apply this to a matrix, but does not work
with a vector...

a = randi([1 31],8,8);
b = randi([1 31],8,8);
a( a < b ) = b;

Greetings,
Pedro
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Vectorization with max / min in matrix

Pedro-2
On Sun, Mar 3, 2013 at 4:19 PM, Pedro <[hidden email]> wrote:

> Hi,
>
> I have two vectors/matrix, and I want to get his minimum values for
> every position without doing for/while/etc statement
>
> But there is a problem:
>
> a = randi([1 31],1,10);
> b = randi([1 31],1,10);
> a( a < b ) = b;
> error: A(I) = X: X must have the same size as I
>
> I don't know why doesn't work, have the same size (perhaps it is not a
> bug because is the same error in Matlab).
> What is going wrong?
>
> The original idea was to apply this to a matrix, but does not work
> with a vector...
>
> a = randi([1 31],8,8);
> b = randi([1 31],8,8);
> a( a < b ) = b;
>
> Greetings,
> Pedro

Ok!
This can be done with min function

use:
min(a,b)
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Vectorization with max / min in matrix

Nir Krakauer-2
or, less simply,

a( a < b ) = b( a < b );


On Sun, Mar 3, 2013 at 10:29 AM, Pedro <[hidden email]> wrote:

> On Sun, Mar 3, 2013 at 4:19 PM, Pedro <[hidden email]> wrote:
>> Hi,
>>
>> I have two vectors/matrix, and I want to get his minimum values for
>> every position without doing for/while/etc statement
>>
>> But there is a problem:
>>
>> a = randi([1 31],1,10);
>> b = randi([1 31],1,10);
>> a( a < b ) = b;
>> error: A(I) = X: X must have the same size as I
>>
>> I don't know why doesn't work, have the same size (perhaps it is not a
>> bug because is the same error in Matlab).
>> What is going wrong?
>>
>> The original idea was to apply this to a matrix, but does not work
>> with a vector...
>>
>> a = randi([1 31],8,8);
>> b = randi([1 31],8,8);
>> a( a < b ) = b;
>>
>> Greetings,
>> Pedro
>
> Ok!
> This can be done with min function
>
> use:
> min(a,b)
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Vectorization with max / min in matrix

Bob Walton
In reply to this post by Pedro-2
Pedro-2 wrote
Hi,

I have two vectors/matrix, and I want to get his minimum values for
every position without doing for/while/etc statement

But there is a problem:

a = randi([1 31],1,10);
b = randi([1 31],1,10);
a( a < b ) = b;
error: A(I) = X: X must have the same size as I

I don't know why doesn't work, have the same size (perhaps it is not a
bug because is the same error in Matlab).
What is going wrong?

The original idea was to apply this to a matrix, but does not work
with a vector...

a = randi([1 31],8,8);
b = randi([1 31],8,8);
a( a < b ) = b;

Greetings,
Pedro
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Try:

a(a<b)=b(a<b);

The number of elements assigned on the left hand side and the number of elements supplied on the right hand side need to be the same.
--
Bob Walton
Loading...