Fortran to Matlab converting the "where" statement

Hi,
I would like to know how is possible to convert the particular statement from fortran to matlab
where (abs(A-B)>e)
no_converge=1
elsewhere
no_converge=0
end where
A and B are arrays of some particular dimensions, and e is a scalar. I have to say that I am not that familiar with either programming languages. However, what I understood from some sources the particular statement takes the element per element difference of those arrays and evaluates whether the difference is higher or lower from a particular scalar, e. For this example, assume that it is very small, e.g e=0.0001.
I have used the f2matlab but it does very poor job on this fortran statement. In case it helps this is a code from F90.
I am wondering whether the quivalent for a matlab is something like this:
if abs(A-B)>e
no_converge=1 ;
else
no_converge=0 ;
end
However, I am not sure whether this is quite general or not. In other words, i am not sure for whatever the condition is or the A's and B's a simple "if" statement does the trick.
I will really appreciate any suggestions here. In case that it helps, this part of the code, is very crucial since it checks whether a whole distribution converges to a particular distribution that is needed.
Many thanks

 Accepted Answer

no_converge= zeros(size(A));
no_converge( abs(A-B) > e ) = 1;
or
no_converge= double( abs(A-B) > e );
or if you can use no_converge as a logical instead of a double:
no_converge= abs(A-B) > e;

6 Comments

Thanks for the swift reply. In the fortran code, the declaration for no_converge is an integer and is used in later sections of the code as such. So, I am wondering whether what you suggest me is still consistent. My own conversion, as I wrote it in the question is wrong you think ?
Your own conversion, as written, does not do what you think it does. The expression
abs(A-B)>e
results in a logical array. When you use an array in an if test in MATLAB, the check is true if the array is non-empty and all the elements of the array are non-zero. Then, depending on that check, no_converge is created as a scalar with the value of either 1 or 0. This is not what is intended in the original Fortran code. I am guessing that no_converge is an array the same size as A and B. Is that true? What is intended is that for each element of the logical result of abs(A-B)>e that is true, set the corresponding element of no_converge to 1, and set all other elements of no_converge to 0. So you need to use one of my formulations which produce an array result for no_converge . The first two give a double result, the last one gives a logical result. Use whichever method makes sense for your downstream code.
Thanks for the explanations. But just a short clarification, the authors of the Fortran code, have declared the no_converge as an integer. So is NOT an array of the same size of either A or B. It's sole purpose is to "switch" on or off some other loops. In that case, the second logical result you are suggesting seems more consistent am i right ?
What do you want to happen if A is within e of B for some elements (say the 1,3,4,6th elements) and farther away from B than e for other elements (say, 2,5,7,8,9 etc.)?
@Safis: What you have written makes absolutely no sense to me at all. The WHERE construct is specifically set up to do element-by-element assignments to arrays controlled by the logical array mask. The variable being assigned to, no_converge, only makes sense in this context if it is an array. Can you double check your code? In addition to the INTEGER statement, is no_converge also part of a separate DIMENSION statement?
@James Tursa: You are right, the authors of the code indeed have declared the no_converge as an array of the same dimensions as in A and B. I got confused because they also have another no_converge_f variable declared as an integer. I now i believe I got it. Thanks

Sign in to comment.

More Answers (0)

Categories

Asked:

msh
on 10 Jun 2014

Edited:

on 11 Jun 2014

Community Treasure Hunt

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

Start Hunting!