Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Embedded Malab 'Find' equivalent?

Subject: Embedded Malab 'Find' equivalent?

From: Mark

Date: 23 Oct, 2007 00:23:07

Message: 1 of 3

Hi experts,

Does anyone know how to implement a 'find' alternative in
Embedded Matlab.

I have a lot of large arrays in Stateflow and want to
filter and sort them for a range of criteria.

I need the indices from the searches to get back to the
original data, hence:

 find(A == 20)

would give me the indices that I can then use to index into
other arrays.

Any thoughts greatley appreciated.


Mark.

Subject: Embedded Malab 'Find' equivalent?

From: Michael Hosea

Date: 23 Oct, 2007 01:20:13

Message: 2 of 3

Mark,

You basically have to work around the fixed-size restriction.
Unfortunately, that's going to limit some of the things you can do with the
library functions, but you can make things work with your own functions.
One way is to use a fixed size output and also return the number of "active"
elements. Here's an example.

function y = foo(x) %#eml
% Calculate the average of elements greater than 0.5.
[idx,n] = myfind(x>0.5);
% Note that we can't use MEAN or SUM on x(idx).
y = 0;
for k = 1:n
    y = y + x(idx(k));
end
if n > 0
    y = y / double(n);
end

function [idx,n] = myfind(b)
% FIND-like function.
% Input B is a logical array.
% Output IDX is a column vector with numel(b) elements.
% Output N is the number of elements of IDX in use.
n = uint32(0);
idx = zeros(numel(b),1,'uint32');
for k = 1:numel(b)
    if b(k)
        n = n + 1;
        idx(n) = k;
    end
end

I've taken the liberty here to make the idx vector 'int32' instead of
double.
--
Mike

"Mark " <medwar19.nospam@hotmail.com> wrote in message
news:ffjetb$en7$1@fred.mathworks.com...
> Hi experts,
>
> Does anyone know how to implement a 'find' alternative in
> Embedded Matlab.
>
> I have a lot of large arrays in Stateflow and want to
> filter and sort them for a range of criteria.
>
> I need the indices from the searches to get back to the
> original data, hence:
>
> find(A == 20)
>
> would give me the indices that I can then use to index into
> other arrays.
>
> Any thoughts greatley appreciated.
>
>
> Mark.

Subject: Embedded Malab 'Find' equivalent?

From: Mark

Date: 23 Oct, 2007 14:54:08

Message: 3 of 3

"Michael Hosea" <mhosea@mathworks.com> wrote in message
<ffji8d$42h$1@fred.mathworks.com>...
> Mark,
>
> You basically have to work around the fixed-size
restriction.
> Unfortunately, that's going to limit some of the things
you can do with the
> library functions, but you can make things work with your
own functions.
> One way is to use a fixed size output and also return the
number of "active"
> elements. Here's an example.
>
> function y = foo(x) %#eml
> % Calculate the average of elements greater than 0.5.
> [idx,n] = myfind(x>0.5);
> % Note that we can't use MEAN or SUM on x(idx).
> y = 0;
> for k = 1:n
> y = y + x(idx(k));
> end
> if n > 0
> y = y / double(n);
> end
>
> function [idx,n] = myfind(b)
> % FIND-like function.
> % Input B is a logical array.
> % Output IDX is a column vector with numel(b) elements.
> % Output N is the number of elements of IDX in use.
> n = uint32(0);
> idx = zeros(numel(b),1,'uint32');
> for k = 1:numel(b)
> if b(k)
> n = n + 1;
> idx(n) = k;
> end
> end
>
> I've taken the liberty here to make the idx
vector 'int32' instead of
> double.
> --
> Mike
>
> "Mark " <medwar19.nospam@hotmail.com> wrote in message
> news:ffjetb$en7$1@fred.mathworks.com...
> > Hi experts,
> >
> > Does anyone know how to implement a 'find' alternative
in
> > Embedded Matlab.
> >
> > I have a lot of large arrays in Stateflow and want to
> > filter and sort them for a range of criteria.
> >
> > I need the indices from the searches to get back to the
> > original data, hence:
> >
> > find(A == 20)
> >
> > would give me the indices that I can then use to index
into
> > other arrays.
> >
> > Any thoughts greatley appreciated.
> >
> >
> > Mark.
>
>


Hi Michael,

That's exactly what I needed, thanks!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us