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:
Position of a min in diagonal

Subject: Position of a min in diagonal

From: Diego Zegarra

Date: 16 Oct, 2008 18:02:03

Message: 1 of 4

Hey guys I need help with this. I have this code,

MTT = cell(12:1)
diagmins = zeros(1,length(MTT));
for n = 1:length(MTT);
    M = MTT{n};
    diagmins(n) = min(diag(M));
end

Now each cell of MTT cell array (e.g. MTT{1}) has a matrix of 120x120 and this code finds the minimum number out of the diagonal of each matrix. Now I need the column (position) where that minimum value is located for each matrix inside the cell array.

Before I used this and gave me both the min and the column but I am not sure how to apply it here or if this way would work at all for what I am trying to do.

[a b] = min(diag(MTT{idx}));

Thanks for your help and I hope I made myself clear.

Subject: Position of a min in diagonal

From: Marcin Pawlik

Date: 16 Oct, 2008 19:20:24

Message: 2 of 4

For a single square matrix M:

n = size(M, 1); % size(M, 1) = size(M, 2) - its square isn't it?
ind = n.*((1:n)-1) + (1:n); % indices on the diagonal
d = diag(M);
ii = min(d) == d;

et voila:

ind(ii)

If it is not enough then:

[i, j] = ind2sub([n, n], ind(ii));

M.

Subject: Position of a min in diagonal

From: Diego Zegarra

Date: 16 Oct, 2008 19:43:01

Message: 3 of 4

Hey Marcin thanks for your help but could you please explain me the code a little bit, what it is doing? Thanks

Subject: Position of a min in diagonal

From: Diego Zegarra

Date: 16 Oct, 2008 20:13:04

Message: 4 of 4

Never mind guys. I found out how to do it with a similar way to what I was doing. I will post the code in case anyone else has this issue. Thanks anyways!

minpos = cell(numMachines,1);
for n = 1:numMachines;
    M = MTT{n};
    [a b] = min(diag(M));
    minpos{n} = [a b];
end

So now I have a cell of numMachines x 1 including the minimum and position for each matrix in each cell.

Tags for this Thread

No tags are associated with 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