Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Minimum value in 3d matrix

Subject: Minimum value in 3d matrix

From: Erik Andersson

Date: 28 Apr, 2010 19:26:05

Message: 1 of 8

Hello!
I have a n*m*t matrix wher a=8, b=20, c=20. In this matrix a have calculated error in different values. Now I want to find the indexvalues, i.e. the values of n, m and t where the minimumerrorvalue of the matrix is?

Thankfull for help!

/E

Subject: Minimum value in 3d matrix

From: Sean

Date: 28 Apr, 2010 19:41:20

Message: 2 of 8

"Erik Andersson" <annerk02@student.umu.se> wrote in message <hra24c$di4$1@fred.mathworks.com>...
> Hello!
> I have a n*m*t matrix wher a=8, b=20, c=20. In this matrix a have calculated error in different values. Now I want to find the indexvalues, i.e. the values of n, m and t where the minimumerrorvalue of the matrix is?
>
> Thankfull for help!
>
> /E


>> [my_min, idx] = min(matrix);
>>[row, col, pag] = ind2sub(size(matrix),idx);

Subject: Minimum value in 3d matrix

From: Walter Roberson

Date: 28 Apr, 2010 19:41:42

Message: 3 of 8

Erik Andersson wrote:
> Hello!
> I have a n*m*t matrix wher a=8, b=20, c=20. In this matrix a have
> calculated error in different values. Now I want to find the
> indexvalues, i.e. the values of n, m and t where the minimumerrorvalue
> of the matrix is?

[minval, idx] = min(TheMatrix(:));
[aidx, bidx, cidx] = ind2sub(size(TheMatrix), idx);

Subject: Minimum value in 3d matrix

From: Bruno Luong

Date: 28 Apr, 2010 19:42:04

Message: 4 of 8

"Erik Andersson" <annerk02@student.umu.se> wrote in message <hra24c$di4$1@fred.mathworks.com>...
> Hello!
> I have a n*m*t matrix wher a=8, b=20, c=20. In this matrix a have calculated error in different values. Now I want to find the indexvalues, i.e. the values of n, m and t where the minimumerrorvalue of the matrix is?
>

% Generate the test matrix
M=rand(8,20,20)

% engine
[minM idx] = min(M(:));
[n m t] = ind2sub(size(M),idx)

% check
minM
M(n,m,t)

% Bruno

Subject: Minimum value in 3d matrix

From: someone

Date: 28 Apr, 2010 19:48:04

Message: 5 of 8

"Erik Andersson" <annerk02@student.umu.se> wrote in message <hra24c$di4$1@fred.mathworks.com>...
> Hello!
> I have a n*m*t matrix wher a=8, b=20, c=20. In this matrix a have calculated error in different values. Now I want to find the indexvalues, i.e. the values of n, m and t where the minimumerrorvalue of the matrix is?
>
> Thankfull for help!
>
> /E

% Something like this (may take some tweaking):
% I'm not sure what your a, b, & c above are.
% First use the min command with the ind option:
[c,ind] = min(A(:));
% then use the ind2sub command
% convert the llinear index to array subscripts:
[n,m,t] = ind2sub(size(A),ind)

Subject: Minimum value in 3d matrix

From: Bruno Luong

Date: 28 Apr, 2010 20:08:20

Message: 6 of 8

OK, as everyone come up to the same solution; here is a different method without using sub2ind

M=rand(8,20,20)

% Engine
[M3 idx3] = min(M, [], 3);
[M2 idx2] = min(M3, [], 2);
[M1 idx1] = min(M2, [], 1);
idx1
idx2=idx2(idx1)
idx3=idx3(idx1,idx2)

% Check
M(idx1,idx2,idx3)
min(M(:))

% Bruno

Subject: Minimum value in 3d matrix

From: Bruno Luong

Date: 28 Apr, 2010 21:43:04

Message: 7 of 8

In recursively way just for fun:

function varargout = ndmin(M)
nd = ndims(M);
if nd==2 && size(M,2)==1
    [~, varargout{1}] = min(M, [], 1);
else
    [M I] = min(M, [], nd);
    [varargout{1:nd-1}] = ndmin(M);
    varargout{nd} = I(varargout{1:nd-1});
end
end % ndmin



%% Command line
M=rand(8,20,20);
[n m t]=ndmin(M)

% Bruno

Subject: Minimum value in 3d matrix

From: Matt J

Date: 29 Apr, 2010 06:19:04

Message: 8 of 8

"Erik Andersson" <annerk02@student.umu.se> wrote in message <hra24c$di4$1@fred.mathworks.com>...
> Hello!
> I have a n*m*t matrix wher a=8, b=20, c=20. In this matrix a have calculated error in different values. Now I want to find the indexvalues, i.e. the values of n, m and t where the minimumerrorvalue of the matrix is?
=======

Are you sure it's not enough just to find the linear lindex of the minimizer?

[trash, LinearIndex]=min(TheMatrix(:));

If so, it could save you computation.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us