finding a minimum from multiple matricies

2 ビュー (過去 30 日間)
jenka
jenka 2012 年 9 月 17 日
Hi everybody, I have a few large matricies of equal size. At each location, I would like to find minimum and its index. Note, essentially an index in this case would correpsond to a particular matrix. Say I have two matricies first=[2 3 4] and second=[4 5 2] Then at location (1,1) the minimum would be 2 with index=1 as to correcpond to the first matrix. I am wondering if there is a way to do it without runing double for loop? Thanks!
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 17 日
how many matrix do you want to compare?

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

回答 (5 件)

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 17 日
Let your three matrices in 3D array M
M = randi(234,5,5,3); % initial array
[v,ii] = min(M,[],3);

Sean de Wolski
Sean de Wolski 2012 年 9 月 17 日
first=[2 3 4];
second=[4 5 2]
val = min(first,second);
idx = (second==val)+1

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 17 日
編集済み: Azzi Abdelmalek 2012 年 9 月 17 日
A={rand(4,5),rand(4,5),rand(4,5),rand(4,5)};
[c,idx1]=cellfun(@(x) min(x(:)),A)
[value,matnum]=min(c) % value = minimum and matnum= matrix number
idx=idx1(matnum)
nA=4 % nA, each matrix is nA x mA
n=mod(idx,nA)
if n==0
n==nA
end
m=ceil(idx/nA) % the location is (n,m)

Image Analyst
Image Analyst 2012 年 9 月 17 日
編集済み: Image Analyst 2012 年 9 月 17 日
Here's a demo for 4 small matrices. You can adapt it for more matrices, and for larger matrices:
format compact;
m1 = rand(5,5)
m2 = rand(5,5)
m3 = rand(5,5)
m4 = rand(5,5)
% Now we have our several "large" matrixes.
% Concatenate them
m3D = cat(3, m1, m2, m3, m4);
% Find the min and a map of what matrix it occurred in.
[min2DImage matrixWhereItOccurs] = min(m3D, [], 3)
Results:
m1 =
Columns 1 through 3
0.683715572408358 0.640717922965926 0.740032327987778
0.132082955713563 0.328814214756803 0.234826914747904
0.722724539656766 0.653812022595774 0.734957541696052
0.110353480642349 0.749131463103519 0.970598525086614
0.117492852151833 0.583185731454876 0.866930291751916
Columns 4 through 5
0.0862345298634963 0.789363943641905
0.366436616319199 0.367652918437877
0.369198804330018 0.206027859505195
0.685028472661609 0.0866665473955323
0.597941635383889 0.771933917099107
m2 =
Columns 1 through 3
0.20567452146476 0.48448037239818 0.237373019705579
0.388271631047802 0.151845525116267 0.530872257027928
0.551778531957227 0.781931966588002 0.0914987313394122
0.2289532520231 0.100606322362422 0.405315419880591
0.641940620399187 0.294066333758628 0.104846247115757
Columns 4 through 5
0.112283962156027 0.432484993970361
0.784427890743913 0.69475219461794
0.291570317906931 0.758099275289454
0.603533438750887 0.432642326147101
0.964422667215901 0.655498039803537
m3 =
Columns 1 through 3
0.109755050723052 0.487603775433924 0.673294914108653
0.933759848385332 0.768958264058869 0.429564459251853
0.187460806421687 0.396006745217875 0.451739234904736
0.266178838907639 0.272938794123691 0.609857169290216
0.797830260211597 0.037234634070328 0.0594032968582772
Columns 4 through 5
0.315811438338866 0.0923523387192016
0.772722130862935 0.00782029356933489
0.696432989006095 0.423109385164167
0.12533218110918 0.655573174937914
0.130151450389424 0.722922524692024
m4 =
Columns 1 through 3
0.531209293582439 0.0985940927109977 0.316428999146291
0.108817938273045 0.142027248431928 0.217563309422821
0.631766373528489 0.168251298491528 0.251041846015736
0.126499865329303 0.196248922256955 0.892922405285977
0.134303304313575 0.317479775149435 0.703223224556291
Columns 4 through 5
0.555737942719387 0.706715217696931
0.184433667757653 0.557788966754876
0.212030842532321 0.313428989936591
0.0773468081126768 0.166203562902151
0.913800410779568 0.622497259279895
min2DImage =
Columns 1 through 3
0.109755050723052 0.0985940927109977 0.237373019705579
0.108817938273045 0.142027248431928 0.217563309422821
0.187460806421687 0.168251298491528 0.0914987313394122
0.110353480642349 0.100606322362422 0.405315419880591
0.117492852151833 0.037234634070328 0.0594032968582772
Columns 4 through 5
0.0862345298634963 0.0923523387192016
0.184433667757653 0.00782029356933489
0.212030842532321 0.206027859505195
0.0773468081126768 0.0866665473955323
0.130151450389424 0.622497259279895
matrixWhereItOccurs =
3 4 2 1 3
4 4 4 4 3
3 4 2 4 1
1 2 2 4 1
1 3 3 3 4

jenka
jenka 2012 年 9 月 18 日
You, guys, are soooooooooooooo great! Awesome! and thank you!

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by