help-octave
[Top][All Lists]

 From: Geordie McBain Subject: Re: Easy about Matrices Date: Mon, 18 Apr 2005 20:12:28 -0500 User-agent: Mutt/1.5.8i

```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.

Geordie McBain
www.aeromech.usyd.edu.au/~mcbain

On Mon, Apr 18, 2005 at 06:55:38PM -0500, Mike Miller wrote:
> On Mon, 18 Apr 2005, Quentin Spencer wrote:
>
> >I'm not completely sure if this is what you're looking for, but maybe
> >this example helps:
> >
> >x = randn(1,1000);
> >x_positive = x(find(x>=0));
> >x_negative = x(find(x<0));
>
>
> That seems like it must have been an excellent answer for Alvaro.  I have
> a related question.  Suppose I have a vector like this:
>
> X=[1, 3, 9, 2, 4, 2, 7, 5, 3, 2, 9, 7]';
>
> And suppose I want the indices of all elements of X that are in this
> vector Y:
>
> Y=[2, 3, 9]';
>
> 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.)
>
> Mike
>
>
>
> -------------------------------------------------------------
> 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
> -------------------------------------------------------------

-------------------------------------------------------------
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
-------------------------------------------------------------

```