help-octave
[Top][All Lists]

## Re: Easy about Matrices

 From: Peter Bodin Subject: Re: Easy about Matrices Date: Tue, 19 Apr 2005 11:22:53 +0200 (CEST) User-agent: SquirrelMail/1.4.4-2

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

>> find(ismember(X,Y))
ans =

2
3
4
6
9
10
11

provided that you have the functions from octave-forge.

HTH

PB

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

```

reply via email to