Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

loop-free comparison of different sized vectors

8 views
Skip to first unread message

José Ignacio Royo Prieto

unread,
Mar 10, 2014, 7:16:07 AM3/10/14
to
Hello, everybody

I want to compare all the elements of two different sized vectors without using 'for' loops.

More precisely, if v and w are vectors of lengths n and m, respectively,
I want to produce a matrix A of dimensions nxm such that
A(i,j)=1 if v(i)>w(j) and 0 otherwise.

Of course, one can do that just using a loop going through all the elements of one of the vectors, something like

for i=1:length(v)
A(i,:)=v(i)>w
end

but I was looking for a "vectorialized" Octave command to do it more efficiently. It is a pity that just typing v>w of v.>w Octave prompts an error, and does not do what I intend to ;-)

Thanks you all for your time and attention.

pek...@arcor.de

unread,
Jun 8, 2017, 12:59:46 AM6/8/17
to
Hello José,

I think that you could solve your problem like this:

a= rand(1,5)
b= rand(1,7)
diag(a>b')

gives you the elements in which a(i) > b(i), for instance.

Best regards
Peter

Andreas Weber

unread,
Jun 16, 2017, 2:15:40 AM6/16/17
to
Am 08.06.2017 um 06:59 schrieb pek...@arcor.de:
> Am Montag, 10. März 2014 12:16:07 UTC+1 schrieb José Ignacio Royo Prieto:
>> More precisely, if v and w are vectors of lengths n and m, respectively,
>> I want to produce a matrix A of dimensions nxm such that
>> A(i,j)=1 if v(i)>w(j) and 0 otherwise.

A = v'>w;

Does what you want. This uses automatic broadcasting, you can also use
it explicitely with "bsxfun"

-- Andy
0 new messages