Hello, How do i return the smallest positive value from a matrix?
29 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (2 件)
John D'Errico
2016 年 3 月 31 日
So
min(m_dot(wd > 0))
does not work? I'd try it anyway. :)
2 件のコメント
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
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 件のコメント
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 Center および File Exchange で Author Block Masks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!