How to make conditional calculation?
显示 更早的评论
I have two vectors, contained in matrix M, each with a dimension of 1000 by 1. Each vector contains integer numbers. The values in one vector (call it vector two) is generated in way that it depends on the values in the other vector (call it vector one). I want to calculate the variance of the numbers in vector two if the values are below a certain threshold in vector one. I try to use the code below, but it returns nothing. Where am I making the mistake?
if M(:,1) < 0
var(M(:,2))
end
采纳的回答
更多回答(1 个)
Walter Roberson
2018-2-24
if M(:,1) < 0
means the same thing as
if all(M(:,1) < 0)
which is probably not true.
Look up "logical indexing"
2 个评论
Snoopy
2018-2-24
Walter Roberson
2018-2-24
That is one approach, but it is more efficient to use logical indexing. https://www.mathworks.com/help/matlab/math/matrix-indexing.html#bq7egb6-1
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!