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

조회 수: 29 (최근 30일)
Ieuan Lewis
Ieuan Lewis 2016년 3월 31일
댓글: Image Analyst 2016년 4월 1일
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

답변 (2개)

John D'Errico
John D'Errico 2016년 3월 31일
So
min(m_dot(wd > 0))
does not work? I'd try it anyway. :)
  댓글 수: 2
Ieuan Lewis
Ieuan Lewis 2016년 3월 31일
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 2016년 4월 1일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 3월 31일
편집: Image Analyst 2016년 3월 31일
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
Ieuan Lewis
Ieuan Lewis 2016년 4월 1일
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 2016년 4월 1일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Author Block Masks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by