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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Matt Learner
on 28 Nov 2012

consider the following example, Suppose if I have a cell notation

q=cell(5,1);

And if I have different dimensions for each cell, for example

q{1} = [6 7 9 4 0 0 1 2 3 1]; q{2} = [3 5 0 4 0 0 1 2 8 1]; q{3} = [3 5 0 5 4 3 4 2 6 4 0 0 1 2 8 1]; q{4} = [4 0 0 1 2 8 1]; q{5} = [4 0 0 0 2 8];

Now if I wish to find the **latest cell location** containing **minimum non zero value**, How can I find it?

In the above example, **minimum non zero value is 1** and it is present in all cells, that is, q{1} q{2} q{3} q{4} **except q{5}**. So here the **latest cell containing 1 is q{4}** and 1 is located at **4th and 7th place of q{4}**. I wish to get the location details mentioning that value 1 is present in q(4,4) and q(4,7). How can I find that?

*No products are associated with this question.*

Answer by Matt Fig
on 28 Nov 2012

Accepted answer

L = cellfun(@(x) find(x==1),q,'Un',0);

Now L has all the information you need.

Show 9 older comments

Matt Fig
on 28 Nov 2012

This should do the correct thing even if you do have negatives.

mn = cellfun(@(x) min(x(x>0)),q,'Un',0); mn = min([mn{:}]) % Show the minimum positve value. L = cellfun(@(x) find(x==mn),q,'Un',0); idx = find(~cellfun('isempty',L),1,'last') % Which cell has the min. L = L{idx} % And the positions.

Matt Fig
on 28 Nov 2012

Or, simpler

mn = cellfun(@(x) min(x(x>0)),q,'Un',0); [mn,idx] = min(fliplr([mn{:}])); mn % Show the minimum positve value. idx = length(q) - idx + 1 % Which cell has the min. L = cellfun(@(x) find(x==mn),q,'Un',0); L = L{idx} % And the positions.

Matt Learner
on 28 Nov 2012

**Thank you very much for your help**. It completely solved my issue.

Thanks again. :)

Answer by Vishal Rane
on 28 Nov 2012

The first thing that comes to mind is

find(q{n} > 0)

It will you give you locations of elements matching the condition ' > 0' in q{n}, else it returns empty.

You could use that to build your logic.

Refer Link for examples on the *find* command.

## 0 Comments