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
2012년 11월 28일
L = cellfun(@(x) find(x==1),q,'Un',0);
Now L has all the information you need.
댓글 수: 14
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?
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
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
2012년 11월 28일
편집: Matt Learner
2012년 11월 28일
Please could anyone comment on the above post?
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
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
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
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
replace the '1' with lim:
lim = cat(2,q{:}); lim = min(lim(lim>0))
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.
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.
Thank you very much for your help. It completely solved my issue.
Thanks again. :)
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
You are load()'ing the same file each time, and it has no data in it.
추가 답변 (1개)
Vishal Rane
2012년 11월 28일
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.
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
