find the location of minimum value

i have matrix with 4 variables, A=mintemp(a,b,c,d). i have find the minimum value especially at V=mintemp(:,:,1,1) dan the minimum value is out=min(V(:)). now, how to find the location / the coordinates the minimum value ?
if the matrix just two variables, i can the identify the coordinates with this :
A=magic(4);
out=min(A(:));
aa=1;
bb=1;
for xx=1:4
for yy=1:4
if(out==1)
aa=xx;
bb=yy;
end
end
end
i really have no idea if the matrix have 4 variables.

1 commentaire

Azzi Abdelmalek
Azzi Abdelmalek le 14 Fév 2013
What is mintemp? and what do you mean by: the matrix have 4 variables.

Connectez-vous pour commenter.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 14 Fév 2013
Modifié(e) : Azzi Abdelmalek le 14 Fév 2013

0 votes

Edit
A=rand(4,4,4,4)
[ii1,ii2,ii3,ii4]=size(A)
count=0
id=zeros(ii3*ii4,4);
for k=1:ii3
for p=1:ii4
count=count+1;
v=A(:,:,k,p);
[val,idx]=min(v(:));
[id1,id2]=ind2sub(size(v),idx);
minval(count)=val;
idx1(count)=id1;
idx2(count)=id2;
idx3(count)=k;
idx4(count)=p;
id(count,:)=[id1 id2 k p] % correspondant indices
end
end

11 commentaires

Internazionale
Internazionale le 14 Fév 2013
if A=rand(4,4,4,4) and run the program the variable x is part like (:,:,1,1),(:,:,1,2) , ... until (:,:,4,4). After i find the minimum value every part, how to find the coordinates of minimum value every part ?
Azzi Abdelmalek
Azzi Abdelmalek le 14 Fév 2013
Modifié(e) : Azzi Abdelmalek le 14 Fév 2013
A=rand(4,4,4,4)
[ii1,ii2,ii3,ii4]=size(A)
count=0
for k=1:ii3
for p=1:ii4
count=count+1;
v=A(:,:,k,p);
[val,idx]=min(v(:));
[id1,id2]=ind2sub(size(v),idx);
minval(count)=val;
idx1(count)=id1;
idx2(count)=id2;
idx3(count)=k;
idx4(count)=p;
end
end
Internazionale
Internazionale le 14 Fév 2013
can we make every output the minimum value, the coordinates will go serially . example if minval=0,0087 so the output serailly (1,2,1,4).
Internazionale
Internazionale le 14 Fév 2013
i mean serially, sorry for the miswords
Thorsten
Thorsten le 14 Fév 2013
I changed my answer, minserial gives the desired output.
Azzi Abdelmalek
Azzi Abdelmalek le 14 Fév 2013
Modifié(e) : Azzi Abdelmalek le 14 Fév 2013
Internazionale. Have you tried the above code?
Internazionale
Internazionale le 14 Fév 2013
i have tried your last program. it works, but same with my last question, example, if we get minimum value at (:,:,1,2), the coordinates will get automatically with pattern (1,3,1,2). can we do that ?
Azzi Abdelmalek
Azzi Abdelmalek le 14 Fév 2013
Modifié(e) : Azzi Abdelmalek le 14 Fév 2013
That's what the code do! (previous comment) idx1,idx2,idx3,idx4 in the code correspond to 1,3,1,2
Internazionale
Internazionale le 14 Fév 2013
but, if you want to see the coordinates, we must open the workspace (idx1,idx2,idx3,idx4 separately). can we merge it all direct if we want to see the coordinates of minimum value ?
Azzi Abdelmalek
Azzi Abdelmalek le 14 Fév 2013
Modifié(e) : Azzi Abdelmalek le 14 Fév 2013
Look at the edited answer. It's the variable id
Internazionale
Internazionale le 25 Fév 2013
on the variable id [id1 id2 k p], next problem is, i want to make variable ei and fi.
ei = k-(id1*0.5) and fi = p-(id2*0.5). so the output is 64 value of ei and 64 value of fi.

Connectez-vous pour commenter.

Plus de réponses (2)

Thorsten
Thorsten le 14 Fév 2013
That's easy
X = rand(4, 4, 4, 4);
[val ind] = min(X(:));

2 commentaires

Internazionale
Internazionale le 14 Fév 2013
i mean the coordinates of the minimum value. so, after we find the minimum value, we must search the coordinates. i think the answer is, example mincoordinates=(1,2,1,4).
Thorsten
Thorsten le 14 Fév 2013
Modifié(e) : Thorsten le 14 Fév 2013
[i j k l] = ind2sub(size(X), ind);

Connectez-vous pour commenter.

Thorsten
Thorsten le 14 Fév 2013
Modifié(e) : Thorsten le 14 Fév 2013

0 votes

R = rand(4,4,4,4);
for i = 1:size(R, 3)
for j = 1:size(R, 4)
[min_val(i, j) min_ind(i, j)] = min(flatten(R(:,:, i, j)));
[a b] = ind2sub(size(R), min_ind(i,j));
minserial(end+1, :) = [i j a b];
end
end
With the convenience function flatten defined as
function y = flatten(x)
y = x(:);

Community Treasure Hunt

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

Start Hunting!

Translated by