From: Ben Williams on
Hi there,

I have an array of data within which I would like to be able to find the linear index of elements that satify a given condition.

For example:

data = [1,2,3,4,5,6,7,8,9,10];

find linear index of elements where data > 3 and data <6 . Pass linear index of these elements to a new arrray.

I am sure it must be simple, but does anybody know how to do this?

Thanks,

Ben
From: Steven Lord on

"Ben Williams" <benjamin.williams(a)remove.this.plymouth.ac.uk> wrote in
message news:hu3h2u$9uc$1(a)fred.mathworks.com...
> Hi there,
>
> I have an array of data within which I would like to be able to find the
> linear index of elements that satify a given condition.
> For example:
>
> data = [1,2,3,4,5,6,7,8,9,10];
>
> find linear index of elements where data > 3 and data <6 . Pass linear
> index of these elements to a new arrray.
> I am sure it must be simple, but does anybody know how to do this?

Are you certain you need the linear indices? If you're going to use them to
index back into data (or into another variable of the same size and shape as
data) then don't use linear indexing -- use logical indexing.

data = 1:10;
logicalIndices = (data > 3) & (data < 6);
valuesInRange4to5 = data(logicalIndices);

If you must obtain linear indices:

linearIndices = find(logicalIndices);

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: us on
"Ben Williams" <benjamin.williams(a)remove.this.plymouth.ac.uk> wrote in message <hu3h2u$9uc$1(a)fred.mathworks.com>...
> Hi there,
>
> I have an array of data within which I would like to be able to find the linear index of elements that satify a given condition.
>
> For example:
>
> data = [1,2,3,4,5,6,7,8,9,10];
>
> find linear index of elements where data > 3 and data <6 . Pass linear index of these elements to a new arrray.
>
> I am sure it must be simple, but does anybody know how to do this?
>
> Thanks,
>
> Ben

one of the solutions

v=[1,2,3,4,5,6,7,8,9,10,4,4,10,4,5];
lix=find(v>3 & v<6)
% lix = 4 5 11 12 14 15

us