How to do Matrix calculations IF certain conditions are present.

2 views (last 30 days)
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.

Answers (1)

Walter Roberson
Walter Roberson on 23 Jun 2013
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 ?

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!