Matlab takes only 4 values of a digit for calculation - matlab

I wrote a code to combine zeros in a matrix.For example,if there are 3 zeros,then l will be equal to 0.3 and 0.3 will be put into the matrix B.If any other numbers are there, then they are put into matrix B without any change.
k=1;l=0;i=1;
A=J;
xx=size(A,2);
while(i<=xx)
if(A(i)~=0)
if(i==1 || A(i-1)~=0)
B(k)=A(i);
k=k+1;
i=i+1;
else
if(A(i-1)==0)
str=num2str(l);
if(mod(l,10)==0)
str=fliplr(str);
str2=strcat('0.',str);
else
str2=strcat('0.',str);
end;
num=str2num(str2);
B(k)=num;
k=k+1;
B(k)=A(i);
k=k+1;
i=i+1;
l=0;
end;
end;
else
l=l+1;
i=i+1;
end;
end;
if(l~=0)
str=num2str(l);
str2=strcat('0.',str);
num=str2num(str2);
B(k)=num;`
end;
Here,the value of 'l' became equal to 23440 and only the value 2344 was taken.So instead of 0.04432, matlab is storing this as 0.2344.How do I make 'l' take the value 23440?

If the array ends in zeros (or contains only zeros), the statement if(mod(l,10)==0) ... is not executed. Instead the branch under if(l~=0) at the end of the script is executed, and the function fliplr is not used there.
You should probably write a function to take l as an input and return the number that should be appended to B. Then you can call that function in both places where you needed to deal with zeros. (Also I would rename l to something that looks less like 1, but that's not why the script doesn't work.)

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 to compute the number of operations of jacobi iteration in matlab

I want to compute the number of operations of jacobi iteration in matlab
I do not know how to do it !!!
Can you help me ?
Thanks
Here is my code for newton method :
b=zeros(30,1);
b(6)=2;
alpha=1;
A=zeros(30,30);
A(1,1)=-(2+alpha);
A(1,2)=1;
for ii=2:29
A(ii,ii-1)=1
A(ii,ii)=-(2+alpha)
A(ii,ii+1)=1
end
A(30,29)=1;
A(30,30)=-(2+alpha);
D=diag(diag(A));
R=A-D;
x=zeros(30,1);
for ii=1:100
xk= inv(D)*(b-R*x);
if(norm(xk-x,1)<=10^-5)
break;
end
x=xk;
end
ii
You have counted it, actually.
In case of code
for ii=1:N
%% Code
end
the ii variable works like the counter.
Reading the code step-by-step you:
Define anonymous temporary array, say forII with elements 1 to N with default step of 1.
For each iteration the appropriate element of original forII is assigned to ii
%% Code is executed
New value of ii scalar is assigned.
This behaviour allows:
Use counter=1:N as true counter.
Loop easily over elements of any array, for example for foo='Hello World!'.
Use predefined indices, array elements, etc. for ii=A.
When the for loop is terminated by break or return prompt the elements in queue are not assigned and ii keeps the last assigned value.
You can try
kk=0;
for ii=1:N
kk=kk+1;
if ii==5
break
end
end
disp(['kk = ' num2str(kk) '; ii = ' num2str(ii)]);
kk=0;
for ii='Hello World!'
kk=kk+1;
if ii=='r'
break
end
end
disp(['kk = ' num2str(kk) '; ii = ' num2str(ii)]);

How to delete decimals that have repeating decimals

I'm working on a code able to work data from star of a public catalogue. I've already got arrays of every single variable that is available:
enter code here fid=fopen('000006+2553.txt','r');
i=1;
while 1
tline=fgetl(fid);
if ~ischar(tline), break, end
A{i}=tline;
i=i+1;
end
k=1;
for j=1:1:length(A)
if length(A{j}) > 50 && length(A{j})<=92
B{k}=A{j};
k=k+1;
end
end
m=1;
for l=1:1:length(B)
C=strread(B{l},'%s','delimiter',' ');
HJD(m)=str2num(C{1});
MAG_3(m)=str2num(C{2});
MAG_0(m)=str2num(C{3});
MAG_1(m)=str2num(C{4});
MAG_2(m)=str2num(C{5});
MAG_4(m)=str2num(C{6});
MER_3(m)=str2num(C{7});
MER_0(m)=str2num(C{8});
MER_1(m)=str2num(C{9});
MER_2(m)=str2num(C{10});
MER_4(m)=str2num(C{11});
FRAME(m)=str2num(C{13});
m=m+1;
end
My problem is, that some of the values in the arrays are repeating decimals, like 29.99999 and 99.99999....etc. Since this numbers are the result of saturation of the sensor, they are wrong data, and must be eliminated. Is there anyway I can tell MATLAB to delete this particular numbers? Any help would be appreciated.

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);