
From:  Alvaro Aguilera 
Subject:  Re: Easy about Matrices 
Date:  Tue, 19 Apr 2005 09:59:55 +0200 
On Tue, 19 Apr 2005, Geordie McBain wrote:
> I won't claim this is less awkward, but it does do the job without a
> loop:
>
> octave> X = [1, 3, 9, 2, 4, 2, 7, 5, 3, 2, 9, 7]';
> octave> Y = [2, 3, 9]';
> octave> mod (find (kron (X, ones (1, length (Y))) == kron (Y', ones (length (X), 1))), length (X))
> ans =
>
> 4
> 6
> 10
> 2
> 9
> 3
> 11
>
> octave>
>
> which gives the same ans as your index, below.
Thank you, Geordie. That is a step forward. It would work nicely for
certain matrix sizes but it won't scale well if the X and Y vectors get
very long. By the way, we noted some years ago on this list that
multiplication is faster than kron, when multiplication can be used, so I
think this might work faster than the above:
mod (find (X*ones(1,length(Y)) == ones(length(X),1)*Y'), length (X))
That was a nice tip, Geordie. Thanks again. Of course, if anyone else
has more ideas, I'm all ears!
Mike
>> I can see how to do it with a loop...
>>
>> index=find(X==Y(1)); for i=2:length(Y), index=[index ; find(X==Y(i))]; end
>>
>> ...but can it be done without a loop? (Also, my method is a little
>> awkward and I wouldn't mind hearing about how I could do it better.)

Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html

[Prev in Thread]  Current Thread  [Next in Thread] 