restricting vector using if else loop. How to get rid of false values from the matrix
1 次查看(过去 30 天)
显示 更早的评论
Hi suppose I have a 100x3 matrix representing coordinates in xyz plane. I use an if loop to select particles with a certain x value and y value and create a new matrix.
Eg:
for i=1:length(matrix)
if matrix(i,1)>100 & matrix(i,1)<400 & matrix(i,2)>100 & matrix(i,2)<400
matrix_new(i,1)=matrix(i,1);
matrix_new(i,2)=matrix(i,2);
matrix_new(i,3)=matrix(i,3);
end
end
The new matrix I get has zeros for the conditions that are false. How can I modify this condition so that I get a matrix without zeros.
Thanks
0 个评论
采纳的回答
Walter Roberson
2017-8-30
mask = matrix(:,1)>100 & matrix(:,1)<400 & matrix(:,2)>100 & matrix(:,2)<400;
new_matrix = matrix(mask, :);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!