- A(:,4) == 2 -- Checks to see if the 4th column is equal to 2, returning a vector of true/false booleans.
- A(A(:,4)==2,9) -- That vector is then used as a logical index into A, grabbing only the rows that are true, and only the 9th column
- Then take the (natural) logarithm of those values
How can I extract numbers from a column in a matrix dependent on a number in another column ?
4 ビュー (過去 30 日間)
古いコメントを表示
Þorsteinn Guðmundsson
2020 年 8 月 31 日
コメント済み: Þorsteinn Guðmundsson
2020 年 8 月 31 日
As stated I need to extract numbers from a column in the following matrix.
I want to log the numbers in column 9 into a vector only when the number in column 4 has the value 2. I tried using an if statment but that got me nowhere and it's been awhile since I have used matlab or done any coding.
Any help is greatly appreciated.
0 件のコメント
採用された回答
the cyclist
2020 年 8 月 31 日
編集済み: the cyclist
2020 年 8 月 31 日
If your matrix is A, then
output = log(A(A(:,4)==2,9));
In case it is not obvious what that one line of code is going, work from the "inside out":
If you want to take the log "in place", then do this:
idx = A(:,4)==2;
A(idx,9) = log(A(idx,9));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!