How to do Matrix calculations IF certain conditions are present.
2 次查看(过去 30 天)
显示 更早的评论
I have 2 matrices A and B and want to do A./B But some values in B are zero, producing inf then, in later calculations, NaN.
I think I can find the zero values using a logical matrix (learning it!) but I don't know how to use that information.
How do I do; If value_in_matrix_B is not zero, do A./B
Or more generally, how do I do IF such_and_such_condition applies to a particular position THEN do That;
Jonathan.
0 个评论
回答(1 个)
Walter Roberson
2013-6-23
value_in_matrix_B = B(J,K);
if value_in_matrix_B ~= 0
A ./ value_in_matrix_B
end
If you are talking about doing this using logical indexing, then:
nzB = B ~= 0;
A(nzB) ./ B(nzB)
but what about the places where it is 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!