Hello, How do i return the smallest positive value from a matrix?
29 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
John D'Errico
on 31 Mar 2016
So
min(m_dot(wd > 0))
does not work? I'd try it anyway. :)
2 Comments
John D'Errico
on 1 Apr 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.
Image Analyst
on 31 Mar 2016
Edited: Image Analyst
on 31 Mar 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 Comments
Image Analyst
on 1 Apr 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);
See Also
Categories
Find more on Author Block Masks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!