Hello, How do i return the smallest positive value from a matrix?

29 visualizaciones (últimos 30 días)
Ieuan Lewis
Ieuan Lewis el 31 de Mzo. de 2016
Comentada: Image Analyst el 1 de Abr. de 2016
Hello, I need to return the smallest value of a matrix but given a condition. So in the image i want to return the smallest value in Column 1( code name is m_dot) but only for a postive value of column 3 (code name is wd).
Thanks in advance

Respuestas (2)

John D'Errico
John D'Errico el 31 de Mzo. de 2016
So
min(m_dot(wd > 0))
does not work? I'd try it anyway. :)
  2 comentarios
Ieuan Lewis
Ieuan Lewis el 31 de Mzo. de 2016
I'll try that out thanks. I actually have m_dot(k) and wd(k) . How do i then extract all other data on that k^th row of the matrix? This data will then be passed back to a script file that outputs to excel, but i only need he single row where m_dot is minimum
Thanks
John D'Errico
John D'Errico el 1 de Abr. de 2016
You did not ask for that.
min returns a second argument, the index of the element that was identified. However, then you would need to look back at find(wd>0), a reverse lookup to identify the original row.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 31 de Mzo. de 2016
Editada: Image Analyst el 31 de Mzo. de 2016
This code should do it. Repeat for wd. It's vectorized to operate on the whole column vector, so don't put (k) in there!
% Create sample positive and negative data.
m_dot = rand(10,1) - 0.5
% Find row of smallest magnitude. The value may be negative.
[smallestAbsValue, rowOfSmallestValue] = min(abs(m_dot))
% Get the actual value in case it's negative.
smallestValue = m_dot(rowOfSmallestValue)
% Find row of smallest positive number.
positiveIndexes = m_dot > 0;
[smallestPosValue, rowOfSmallestValue] = min(m_dot(positiveIndexes))
If you have a 2D array of 3 columns, and want the whole row, you can extract just that row like this:
theRow = yourMatrix(rowOfSmallestValue, :);
  2 comentarios
Ieuan Lewis
Ieuan Lewis el 1 de Abr. de 2016
Hello,
I've combined both answers to give , the following code.
m_dot = [0.001,0.02,0.004]' wd = [-1,1,2]'
[smallestAbsValue, rowOfSmallestValue] = min(m_dot(wd>0))
theRow = wd(rowOfSmallestValue, :)
This finds the smallest value of m_dot for when wd is positive but gives me the row when wd is smallest and not m_dot. (i.e returns row 2 instead of row 3) How can i fix that?
Just to confirm, i'm trying to find the value and row when m_dot is smallest as long as, in the same row, wd is postive. Thanks in advance.
Image Analyst
Image Analyst el 1 de Abr. de 2016
I see you've edited your question to make it more clear. So what you have to do is to find the criteria in each column, then AND them together to get where it holds for the combined criteria. So if you want "to return the smallest value in Column 1( code name is m_dot) but only for a positive value of column 3 (code name is wd)." then do this
% Find positive indexes in column 3
% which has already been extracted into wd = matrix(:, 3)
positiveCol3 = wd > 0; % Logical vector
% Now find the min of only those rows of m_dot
% which has already been extracted into m_dot = matrix(:, 1)
minWDotValue = min(m_dot(positiveCol3));
% Now find row(s) in original location. There may be more than 1.
rowsWithMin = find(w_dot == minWDotValue);

Iniciar sesión para comentar.

Categorías

Más información sobre Author Block Masks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by