How to find the location of a minimum value in cell array?

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?

 採用された回答

Matt Fig
Matt Fig 2012 年 11 月 28 日

0 投票

L = cellfun(@(x) find(x==1),q,'Un',0);
Now L has all the information you need.

14 件のコメント

Matt Learner
Matt Learner 2012 年 11 月 28 日
When I used this code, it gave me the location of 1 in all the cells (q{1} = [7,10],q{2}=[7,10],q{3}=[13 16],q{4}=[4,7],q{5}=[]). But I wish to find only the last(latest) cell value, and here it would be q{4} because q{5} doesn't contain value 1 (here let us consider q{1}, q{2} and q{3} as older cells). I need something mentioning that the value 1 is located in q (4,4) and q (4,7) I don't need all cell locations.Could you kindly help me out?
Matt Fig
Matt Fig 2012 年 11 月 28 日
So simply look for the last non-empty return...
L = cellfun(@(x) find(x==1),q,'Un',0);
L = L{find(~cellfun('isempty',L),1,'last')}
Matt Learner
Matt Learner 2012 年 11 月 28 日
編集済み: Matt Learner 2012 年 11 月 28 日
Again thanks a lot. Could you kindly explain the following? I understood the following part of the first line code
L = cellfun(@(x) find(x==1),q)
but what is ('Un',0)? - I understood it as
'uniformOutput', false
Also the second line of code (previous post) gave me the location as 5. However it doesn't give me about the cell info. I need the matlab to show me that q(4)th cell and 4th value is a min non zero value (when compared to all cell's columns) similarly q(4)th cell and 7th value is a min non zero value. That is I need the result to be shown as (4th cell,4 column) and (4th cell,7th column) are the latest cell's minimum (lowest) non zero value when compared to all cell's columns. Can anybody share some insight on this?
I would really appreciate your help and Thanks in advance for your time and patience
Matt Learner
Matt Learner 2012 年 11 月 28 日
編集済み: Matt Learner 2012 年 11 月 28 日
Please could anyone comment on the above post?
Matt Fig
Matt Fig 2012 年 11 月 28 日
Hello,
Yes ('Un',0) is shorthand for ('uniformOutput', false). You can always unwrap a piece of code to get the components.
L = cellfun(@(x) find(x==1),q,'Un',0);
idx = find(~cellfun('isempty',L),1,'last'); % Your 4.
L = L{idx} % Your 4 and 7.
Matt Learner
Matt Learner 2012 年 11 月 28 日
編集済み: Matt Learner 2012 年 11 月 28 日
The above code is true if I know I have 1 as the lowest non zero value, But suppose if i have a different lowest min value in any of the cells, say for example 0.2 (you can take any other non zero value). Then I am not able to get results using the above code.
The question I am trying to ask here is that If I don't know the lowest non zero value present in cells and I want to find it, How can I do that?
Cos when I checked the above code by replacing value 1 by 0.2 in all the cells (Q{1} to q{5}). I didn't obtain the expected result.
I might be really bothering you. But just to let you know, I am trying my best to find details on cellfun in matlab documents and reading the threads with cellfun tags hoping to find some details. I hope you understand my efforts. Even I am trying to write code based on the above code to get the result I want. Till now no good news :(
Thanks in advance
Matt Fig
Matt Fig 2012 年 11 月 28 日
O.k. so let's just be clear so we can get this sorted out. In general will there be any negative numbers in any of the cells? Will the smallest number be the same in all the cells or are we looking for a global smallest positive number? Will you know the smallest number before you look as in your examples?
Matt Learner
Matt Learner 2012 年 11 月 28 日
編集済み: Matt Learner 2012 年 11 月 28 日
Thanks, Here are my answers to your question.
  • There won't be any negetive numbers in any of the cells (as the values stored in each of the columns of every cell are obtained from squaring some value). Note: There might be positive decimal values like 0.2, 0.001, etc
  • We are looking for a global smallest positive number(that means there might be a chance that in some cells there won't be any smallest positive number or even any number at all).
  • I will not be knowing the smallest number and I have to find it's location
Jurgen
Jurgen 2012 年 11 月 28 日
replace the '1' with lim:
lim = cat(2,q{:}); lim = min(lim(lim>0))
Matt Fig
Matt Fig 2012 年 11 月 28 日
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
Matt Fig 2012 年 11 月 28 日
編集済み: Matt Fig 2012 年 11 月 28 日
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
Matt Learner 2012 年 11 月 28 日
Thank you very much for your help. It completely solved my issue.
Thanks again. :)
nadia nadi
nadia nadi 2015 年 8 月 26 日
Dear,
if I want to find minimum matrix from this code what should I do. I tried your code but I couldn't fit it to my code. I have many matrices when I want to find the maximum one by using the size I get the right answer but for minimum matrix I got empty matrix. I used this code
for i=1:10
MaxSubmat=load('MaxSubmatfile.txt');
Remind=load('Remindfile.txt');
ee{i}=MaxSubmat;
aa{i}=Remind;
end
[SizeMaxSubmat,MaxSubmat1]=max(cellfun(@(x) size(x,2),ee ))
[SizeRemind, Remind1]=min(cellfun(@(x) size(x,2),aa ))
MaxSub=ee{MaxSubmat1};
Reminder=aa{Remind1};
sizeReminder=size(Reminder,2)
I don't why it give give Reminder=[] and sizeReminder= 0 for while I know is more than one. could you please help me with this code I will appreciate that a lot.
regards, Nadia
Walter Roberson
Walter Roberson 2015 年 8 月 26 日
You are load()'ing the same file each time, and it has no data in it.

サインインしてコメントする。

その他の回答 (1 件)

Vishal Rane
Vishal Rane 2012 年 11 月 28 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by