Transform fortran code to matlab - matlab

How do i tranform this code below to matlab? I got confused on goto statement.
do 57 i=1,10
statement 1
if(k .eq. nx) then
statement 2
go to 58
end if
57 continue
statement 3
58 continue

Using GOTO command is not considered as a good procedural programming.
Use the following program instead:
i=1;
t=true;
while (i<=10)&&t
statement1;
t=k~=nx;
i=i+1;
end
if t
statement2;
else
statement3;
end

This Fortran Snippet has a very bad code-smell to it.
But here are a few things:
true here seems to be a variable. The correct value for true in Fortran is .TRUE. (or .true.). Assuming that that variable is always .TRUE., then the code can be rewritten very easily:
statement 1
statement 2
And that's it. Your code would immediately jump out of the loop and over statement 3, so each of the statements would only be executed once.
But assuming that true is some variable or expression that has to be re-evaluated in each iteration of the loop, this is a better way:
do i = 1, 10
statement 1
if (true) exit
end do
if (true) then
statement 2
else
statement 3
end if
Now this still assumes that true is a static expression, that is that it won't change it's value between calls.

Related

Variable input to MATLAB function resets involuntarily inside while loop. How can I prevent it?

I have written a function which takes in an integer (int8) as one of the inputs (called iscool). The function runs a while loop and I insert an if-check inside it to break out of the loop. The if-check checks the value of the iscool variable as well and sets the value of imarch to 0 to get out of loop. So basically, the code is something like this.
% Code_snippet
while (imarch == 1)
<some procedures not modifying iscool>
if ((iscool == 0) && (<other condition 1>) && (<other condition 2>))
imarch = 0;
elseif ((iscool == 1) && (<other condition 3>) && (<other condition 4>))
imarch = 0;
end
disp (strcat('Cooling index is: ',num2str(iscool)));
end
The output of the disp command in the first while-loop execution is 0 (which is the input), but it changes to 1 in the subsequent iteration and stays so after that. I have tried removing the if-elseif-end check and the value of iscool stays intact in that case, but I need to have the check in order to be able to get out of the loop. Any sort of help, particularly an insight into why the value might be changing would be great help. Thanks.

if/else statement not executing 'if' statement

I have been trying to print some complex numbers. If the complex number (modTrace) is like 'a-ib' then my code:
modTrace
v = [real(modTrace(:)) imag(modTrace(:))].';
fprintf(fileID,'%e+%ei\n',v);
gives the output as:
2.355387e-13+-7.217925e-13i
To avoid the extra + sign in front of the negative imaginary piece I write:
v = [real(modTrace(:)) imag(modTrace(:))].';
if imag(modTrace(:))>0
fprintf(fileID,'%e+%ei\n',v);
else
fprintf(fileID,'%e%ei\n',v);
end
Now in the output the 'if' is not being executed. So if I have a complex number 'a+ib' it prints
'a bi' and if a complex number is 'a-ib' it prints 'a-bi' according to the else statement.
if I then modify the code like:
v = [real(modTrace(:)) imag(modTrace(:))].';
if imag(modTrace(:))<0
fprintf(fileID,'%e%ei\n',v);
else
fprintf(fileID,'%e+%ei\n',v);
end
then again the 'if' statement in not being executed. So for a complex number 'a-ib' the output is 'a+-bi' and the 'else' statement in being executed correctly.
Could any body please help me to find the output in the correct form?
Thanks.
I think that in this case you are better off using a loop:
for k = 1:length(modTrace)
if imag(modTrace(k))>0
fprintf(fileID,'%e+%ei\n',real(modTrace(k)), imag(modTrace(k)));
else
fprintf(fileID,'%e%ei\n',real(modTrace(k)), imag(modTrace(k)));
end
end
You can't vectorize it that easily because if you pass a vector to if it only evaluates true when (http://www.mathworks.com/help/matlab/ref/if.html):
the result is nonempty and contains all nonzero elements (logical or
real numeric). Otherwise, the expression is false.

Program runs normally in Matlab but won't work in Parallel Toolbox

I recently asked about a related problem here:
https://stackoverflow.com/questions/21171836/storing-matrix-output-in-higher-dimensional-matrix
I now simply want to run the code in a parallel configuration, but when I do I get the following error:
??? Error using ==> parallel_function at 598
Error in ==> OIRE at 136
Undefined function or variable "best_index".
Error in ==> OIRE_MSE_test at 73
parfor t=1:nsims
Error in ==> OIRE_MSE_test at 95
[b_OIRE OIRE_opt_b(:,:,t)]=OIRE(y,x,iter);
The code works fine so long as I drop the matlab pool open/close and the parfor command.
How come this won't run in parallel toolbox?
clc;
n=100;
p=7;
alpha1=0.999999;
error_vol=0.1;
iter=1000;
nsims=200;
OIRE_opt_b=zeros(7,3,nsims);
OIRE_opt_MSE=zeros(1,3,nsims);
OIRE_opt_index=zeros(1,3,nsims);
GIREI_opt_b=zeros(7,3,nsims);
GIREI_opt_MSE=zeros(1,3,nsims);
GIREI_opt_index=zeros(1,3,nsims);
GIREII_opt_b=zeros(7,3,nsims);
GIREII_opt_MSE=zeros(1,3,nsims);
GIREII_opt_index=zeros(1,3,nsims);
LRRE_opt_b=zeros(7,3,nsims);
LRRE_opt_MSE=zeros(1,3,nsims);
LRRE_opt_index=zeros(1,3,nsims);
matlabpool open
x=zeros(n,p);
for i=1:n
z_i4=normrnd(0,1);
x(i,p)=z_i4;
for j=1:p-1
x(i,j)=x(i,j)+alpha1*z_i4;
x(i,j)= x(i,j)+(1-alpha1^2)^(0.5)*normrnd(0,1);
end
end
b_act=[5;1;10;-20;200;30;-2];
parfor t=1:nsims
residuals=normrnd(0,error_vol,n,1);
y=x*b_act + residuals;
y_store(:,t)=y;
y=y_store(:,t);
[b_OIRE OIRE_opt_b(:,:,t) OIRE_opt_MSE(:,:,t) OIRE_opt_index(:,:,t)]=OIRE(y,x,iter);
end
called function
function [b_OIRE OIRE_opt_b OIRE_opt_MSE OIRE_opt_index]=OIRE(y,x,iter)
dim=1;
pool=[10,100,1000,10000,1000000,10000000];
count=0;
[n, p]=size(x);
b=x\y;
b_OIRE = b; % [#1] initialize b_LRRE as b
sigma_sq=((y-x*b)'*(y-x*b))/(n-p); %'
b_act=[1;0;1;1;0;1;1];
econFlag=0;
[U,sigma,V] = svd(x,econFlag);
U1=U(:,1:p);
d=zeros(p,1);
d=diag(d);
alpha=V'*b_OIRE; %'
Delta=sigma.^1;
Delta=diag(Delta);
f=Delta.*alpha;
F=diag(f);
Theta=sum(f);
c=p^2*sigma_sq+p*Theta^2;
g=Theta*sum(alpha);
I=ones(p,1);
a=sigma_sq*I+Theta*f;
b=F*alpha;
k=zeros(p,1);
A=sigma_sq*eye(p)+F.^2;
varRho=(g-a'*pinv(A)*b)*pinv(c-a'*pinv(A)*a);
k=pinv(A)*b-varRho*pinv(A)*a;
K=diag(k);
D=varRho*I*I';
b_OIRE= V*(K+D)*U1'*y;
MSE=(k'*A*k)+(2*varRho*a'*k)-(2*b'*k)+(c*varRho^2)-(2*g*varRho)+(alpha'*alpha);
best_OIRE_MSE=MSE;
best_b_OIRE=b_OIRE;
for jj=1:iter % [## "iter" denotes the iteration number]
alpha=V'*b_OIRE; %'
Delta=sigma.^1; % [Error! not sigma.^2 but sigma.^1]
Delta=diag(Delta);
f=Delta.*alpha;
F=diag(f);
Theta=sum(f);
c=p^2*sigma_sq+p*Theta^2;
g=Theta*sum(alpha);
I=ones(p,1);
a=sigma_sq*I+Theta*f;
b=F*alpha;
k=zeros(p,1);
A=sigma_sq*eye(p)+F.^2;
varRho=(g-a'*pinv(A)*b)*pinv(c-a'*pinv(A)*a);
k=pinv(A)*b-varRho*pinv(A)*a;
K=diag(k);
D=varRho*I*I';
b_OIRE= V*(K+D)*U1'*y;
MSE=(k'*A*k)+(2*varRho*a'*k)-(2*b'*k)+(c*varRho^2)-(2*g*varRho)+(alpha'*alpha);
if(MSE<best_OIRE_MSE)
best_b_OIRE=b_OIRE;
best_OIRE_MSE=MSE;
best_index=jj+1;
end
if( any(jj == pool))
count=count+1;
OIRE_opt_b(:,count)=best_b_OIRE;
OIRE_opt_MSE(count)=best_OIRE_MSE;
OIRE_opt_index(count)=best_index;
end
end
end
matlabpool close
Your problem is that best_index is never defined anywhere other than in the MSE<best_OIRE_MSE if-statement.
This is what I suspect is happening. When you run your code without the parfor, it iterates through sequentially (i.e. jj==1 will always occur before jj==2 etc). Without looking through the intricacies of your code, I suspect this means that MSE<best_OIRE_MSE will always be true before (or on the same iteration as) any(jj==pool) is true. This means that by the time your code reaches the any(jj == pool) if-statement, best_index will always have been set.
The problem comes with the fact that parfor offers no guarantees as to the execution order of your loop. jj==10 might well run before jj==2. This means that there is no guarantee that best_index will be defined by the time it reaches the any(jj == pool) if-statement - hence your error.
My suggestion would be to define best_index outside your main parfor loop to something like -1, and then ignore any cases where OIRE_opt_index is negative.

use debugger in matlab

i want to debug following simplest code in matlab and clarify why it executes always if statement
function testfile(x)
if 3<x<6
disp('in the middle of range');
else
disp('out of range');
end
end
i have used following code for debugger
echo testfile on
testfile(-2)
in the middle of range
testfile(6)
in the middle of range
why it does not execute else statement?i have used following code as a test
5<4<8
ans =
1
so does it mean that writing if statement in this style is wrong?a i understood it is same as if 5<4 || 4<8?then it makes clear for me why it executes only if statement and never reaches to else
5<4<8 is evaluated as (5<4)<8. If we resolve the expression in the parentheses first, we have 0<8, which is true. Test with 5<4==0, which evaluates to true.
What you want to do is check whether x is both bigger than 3 and smaller than 6, i.e.
3<x && x<6

Conditional IF/ELSE Statement in Matlab

I was trying to make a simple statement with Matlab as follows:
if TF==1
disp('One'), break
else continue
end
... ... ...
... ... ...
But even if TF is not 1, when I run the command, it doesn't CONTINUE to the rest of the script!! Any help would be appreciated-- Thanks
The continue statement has a very different meaning. Within a loop, like a for or while loop, continue instructs to skip the current round and continue with the next iteration in the loop. So if you remove continue, you will see the behavior that you are expecting. Here is an example:
for k = 1 : 10
if k == 4
% skip the calculation in the case where k is 4
continue
end
area = k * k;
disp(area);
end
When the loop iterates at k == 4, the block calculating the area of the corresponding square is skipped. This particular example is not very practical.
However, imagine you have a list of ten file names, and you want to process each file in this loop "for k = 1 : 10". You will have to try and open each file, but then if you find out the file does not exist, an appropriate way to handle it would be to print a little warning and then continue to the next file.