Matlab executes the first statement even if is false - matlab

I have the following function in matlab. I am trying to shuffle a matrix. But somehow matlab keeps executing the code from if even if the statement should go to else. And when it comes to else I have to put some aditional code, even if all it does is **i+1. It is normal for matlab or am I missing something?
function magic_matrix = magicsquare1(matrix,n)
magic_matrix=ones(n,n)*(-1);
i=3-1;
j=4+1;
for ki=1:n
for kj=1:n
if(i<1)
i=n;
end
if(j>n)
j=1;
end
if magic_matrix(i,j) == -1
magic_matrix(i,j)=matrix(ki,kj);
%X = sprintf('i=%d j=%d',i,j);
%disp(X)
i=i-1;
j=j+1;
else
i=i+2;
j=j-1;
if(i>n)
i=1;
end
if(j<1)
j=n;
end
magic_matrix(i,j)=matrix(ki,kj);
% X = sprintf('i=%d / j=%d',i,j);
%disp(X)
i=i-1;
j=j+1;
end
end
end

Related

Loop doesn't evaluate correctly in MATLAB

I have a problem with a loop not working correctly for some reason, or maybe it is not the loop itself, but rather something I did not pay attention to. I have been trying to solve the issue for hours now, to no avail.
Here's the code:
syms t;
syms m(t);
Nq=3;
L(1,1)=1;
L(2,1)=2;
L(3,1)=3;
w(1,1)=0.2;
w(2,1)=0.1;
w(3,1)=0.7;
G=1;
for x=1:1:2*Nq
m0val(x)=w(1,1)*L(1,1).^(x-1) + w(2,1)*L(2,1).^(x-1) + w(3,1)*L(3,1 ).^(x-1);
end
ode=diff(m,t)==0;
cond = m(0)==m0val(1);
mf(1)=dsolve(ode,cond);
for x=2:1:2*Nq
ode=diff(m,t)==(x-1)*G*mf(x-1);
cond=m(0)==m0val(x);
mf(x)=dsolve(ode,cond);
end
z=1;
for y=0.2:0.2:1
for x=1:1:2*Nq
mfv(z,x)=subs(mf(x),t,y);
end
for x=1:1:2*Nq+1
P(x,1)=eq(x,1);
end
****for x=1:1:2*Nq+1
if x~=2*Nq+1
P(x,2)=((-1).^(x-1))*mfv(z,x);
else
P(x,2)=0;
end****
end
for y=3:1:2*Nq+1
for x=1:1:2*Nq+2-y
P(x,y)=P(1,y-1)*P(x+1,y-2)-P(1,y-2)*P(x+1,y-1);
end
end
alpha(1)=mfv(z,1);
for x=2:1:2*Nq
alpha(x)=P(1,x+1)/(P(1,x)*P(1,x-1));
end
a(1)=alpha(2);
for x=2:1:Nq
a(x)=alpha(2*x)+alpha(2*x-1);
end
for x=1:1:Nq-1
b(x)=-(alpha(2*x+1)*alpha(2*x)).^0.5;
end
for x=1:1:Nq
Jacobi(x,x)=a(x);
end
for x=1:1:Nq-1
Jacobi (x+1,x)=b(x);
Jacobi(x,x+1)=b(x);
end
[evec,eval]=eig(Jacobi);
for x=1:1:Nq
L(x,z+1)=eval(x,x);
w(x,z+1)=mfv(z,1)*evec(1,x).^2;
end
z=z+1;
end
The bit between * is where it's not working properly, because if I compute let's say P(2,2), it should be equal to (-1)^(1)*mfv(z,2) (where z=1 for the first run). It gives a value of 1 which is the value of P(1,2).
I run your code and I find the issue with P data type. P in your code is logic, therefore, it can not store double or so value.
I declare P before using it in the loop then the code works fine.
z=1;
P = zeros(2,2);% the add line
for y=0.2:0.2:1
for x=1:1:2*Nq
mfv(z,x)=subs(mf(x),t,y);
end
for x=1:1:2*Nq+1
P(x,1)=eq(x,1);
end
for x=1:1:2*Nq+1
if x~=2*Nq+1
P(x,2)=((-1).^(x-1))*mfv(z,x);
else
P(x,2)=0;
end
end

How can I set the number of iterations to run in a recursive function in Matlab?

How can I set the number of iterations to run in a recursive function in Matlab?
I have the following function
function t=m(x1,x2)
if x1<0.5
if x2<0.5
t=(0+m(2*x2, 2*x1))/4;
else
t=(1+m(2*x1, 2*x2-1))/4;
end
else
if x2>=0.5
t=(2+m(2*x1-1, 2*x2-1))/4;
else
t=(3+m(1-2*x2, 2-2*x1))/4;
end
end
end
I want it to perform 10^3 iterations.
I tried with
function t=m(x1,x2,iter)
while iter<=10^3
if x1<0.5
if x2<0.5
t=(0+m(2*x2, 2*x1, iter+1))/4;
else
t=(1+m(2*x1, 2*x2-1, iter+1))/4;
end
else
if x2>=0.5
t=(2+m(2*x1-1, 2*x2-1, iter+1))/4;
else
t=(3+m(1-2*x2, 2-2*x1, iter+1))/4;
end
end
end
end
and in the main
x1=0.3;
x2=0.4;
iter=0;
t=m(x1,x2,iter);
but it gives me several errors. Any help?
Firstly, you shouldn't put that loop inside your function. Repeating is the role of recursion. So change your function to:
function t=m(x1,x2,iter)
t = 0;
if iter > 0
if x1<0.5
if x2<0.5
t=(0+m(2*x2, 2*x1, iter-1))/4;
else
t=(1+m(2*x1, 2*x2-1, iter-1))/4;
end
else
if x2>=0.5
t=(2+m(2*x1-1, 2*x2-1, iter-1))/4;
else
t=(3+m(1-2*x2, 2-2*x1, iter-1))/4;
end
end
end
end
(supposing you want it to return 0 after the last iteration)
Now you can call your function like this:
m(0.3, 0.4, 10^3)
But before that, call:
set(0,'RecursionLimit',10^3+2)
For these inputs, it converges to 0.1808 after only 8 iterations.

How to save results from my script in vectors in Matlab?

I was wondering if someone could help me with MatLab.
Is there a way to save these 2 values count_zero and count_value into 2 vectors.
The part of interest in the following code is inside the while loop, the upper part is irrelevant for this question.
For example all values of count_zero should be saved in vector a=[count_zero count_zero ..] and all values of count_value in vector b=[count_value count_value ...].
This is my code, thanks in advance.
threeminutesofvideo_Youtube;
h=[0:0.5:179];
for idx=1:length(h)
threshold=h(idx);
m =find(threshold-1<=x & x<=threshold);
Y(idx)=sum(y(m));
end
count_zero=0;
count_value=0;
i=1;
while i<length(Y)
if (Y(i)==0)
count_zero=count_zero+1;
i=i+1;
while Y(i)==0 && i<length(Y)
count_zero=count_zero+1;
i=i+1;
end
if i<(length(Y))
count_zero
count_zero=0;
end
if i==(length(Y)) && Y(length(Y))~=0
count_value=1;
count_value
count_value=0;
elseif i==(length(Y)) && Y(length(Y))==0
count_zero=count_zero + 1;
count_zero
count_zero=0;
end
else
count_value=count_value+1;
i=i+1;
while Y(i)~=0 && i<length(Y)
count_value=count_value+1;
i=i+1;
end
if i<(length(Y))
count_value
count_value=0;
end
if i==(length(Y)) && Y(length(Y))~=0
count_value=count_value+1;
count_value
count_value=0;
elseif i==(length(Y)) && Y(length(Y))==0
count_zero=1;
count_zero
count_zero=0;
end
end
end
As far as I have understood you want to memorize the values in a vector, not to save to file don't you?
In this case let us call the vector in which you want to memorize If you know a priori the number of values you want to memorize you can do this
a = NaN*ones(num_of_values,1);
i=1;
while condition
...
a(i) = temp_val;
i = i+1;
end
If you don't know the number of values a priori:
a=[];
i=1;
while condition
...
a = [a;temp_val];
i=i+1;
end
I hope to have been helpful

Code returns minimal m s.t P(X>m)=1/n^2 for binomial experiment

Assume we do an experiment where we throw n balls into n jars. X is an independent variable describes number of balls int the first jar. Build a function returns the smallest integer fulfills P(X>m)<1/n^2.
The distribution is binomial so I wrote the following matlab function:
function m = intpq3(n)
flag=0;
par=1/n^2;
m=0;
P=0;
%Since X is non-neative integer
if(n==1)
m=-1*Inf;
else
while(flag==0 && m<=n)
m=m+1;
P=P+nchoosek(n,m)*(1/n)^m*(1-1/n)^(n-m);
if(1-P<=par)
flag=1;
end
end
end
disp(m)
end
But for every 'n' I give it, it returns either error or n-1. What am I doing wrong?
The following version of your program seems to do what you want. The problem with your version as far as I can tell is that you did not include m=0 into your sum of P, thus 1-P was consistently too large. It's always a good idea to ask the program to spit out numbers and compare to paper-and-pencil calculations.
function [m,P] = intpq3(n,varargin);
if nargin==2
plt=varargin{1};
else
plt = 0;
end
flag=0;
par=1/n^2;
m=0;
P=0;
disp(['1/n^2 = ' num2str(par)]);
%Since X is non-neative integer
if(n==1)
m=-1*Inf;
else
while m<n & flag==0
P=P+nchoosek(n,m)*(1/n)^m*(1-1/n)^(n-m);
disp([' n= ' num2str(n) ' m= ' num2str(m) ' P(X>' num2str(m) ') ' num2str(1-P)])
if(1-P<=par)
flag=1;
end
m=m+1;
end
end
disp(['mselect = ' num2str(m-1)]);
end
The following also helped to diagnose the problem. It shows values of P(X>m) (colored dots) for different m at select values of n. Overlayed as a dashed line is 1/n^2.

Using fprintf to print on several lines in Matlab

Question:
Write a procedure called Print7 to print all integer numbers within the range 0:100 that are divisible by 7. Ten numbers are to be printed on one output line. Hence write a program that invokes that procedure.
This is what I did
file = fopen('print7.dat','r');
x = 1:100
for x=[1:100]
if mod(x,7) == 0;
print7 = [x]
end
end
fprintf('print7 %d\n', print7)
Now it's output becomes the number 98 - which I understand to be the largest number under 100 divisible by 7. But I want a 10xn matrix-like result.
What do I do?
What you are doing stores your result in a variable and overwrites the variable in each iteration. You could print it directly instead like this:
c=0;
for x=[1:100]
if mod(x,7) == 0
fprintf('%3d',x)
c=c+1;
if mod(c,10) ==0
fprintf('\n')
end
end
end
fileID = fopen('print7.dat','r');
for x = 1:100
if(mod(x,7) == 0)
fprintf(fileID,'%d',x);
end %end of if
end %end of for
fclose(fileID);