How to delete decimals that have repeating decimals - matlab

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.

Related

How to plot data dynamically in MATLAB

I am having trouble plot the data dynamically,my goal is to plot data after checking a certain test of 14 days if I entered that condition loop I would like to execute a rectangle between the 1st day and the 14th day.
when I enter that last if loop I already have a xfist,xlast,y first y last. so I can draw a rectangle between them. And then when I pass the 14 days test again I would like to add to the existing plot another rectangle.
Here is my code so far.
The plot lines don't plot anything.
j=1;
while(j<72)
boom=true;
if a13(j)~= b8(j)|| a13(j)>1.1*(b8(j))&& a13(j)<0.9*(b8(j))
elseif a13(j)~=c5(j)|| a13(j)<0.9*(c5(j))&&a13(j)<0.9*(c5(j))
boom=false;
end
Xfirst=[];
Yfirst=[];
Xlast=[];
Ylast=[];
Yfirst=a13(j);
Xfirst=datetime(Date(j));
for i=j+1 :j+14
if a13(i)~= b8(i)|| a13(i)>1.1*(b8(i))&& a13(i)<0.9*(b8(i))
elseif a13(i)~= c5(i) || a13(i)<0.9*(c5(i)) && a13(i)>1.1*(c5(j))
j=i;
boom=false;
break;
end
end
if(boom==true)
Ylast=a13(j+14);
Xlast=New_Date(j+14);
figure (1)
plot(Xfirst,Yfirst)
hold on
plot(Xlast,Ylast)
end
j=j+1;
end
use drawnow inside the loop:
j=1;
while(j<72)
boom=true;
if a13(j)~= b8(j)|| a13(j)>1.1*(b8(j))&& a13(j)<0.9*(b8(j))
elseif a13(j)~=c5(j)|| a13(j)<0.9*(c5(j))&&a13(j)<0.9*(c5(j))
boom=false;
end
Xfirst=[];
Yfirst=[];
Xlast=[];
Ylast=[];
Yfirst=a13(j);
Xfirst=datetime(Date(j));
for i=j+1 :j+14
if a13(i)~= b8(i)|| a13(i)>1.1*(b8(i))&& a13(i)<0.9*(b8(i))
elseif a13(i)~= c5(i) || a13(i)<0.9*(c5(i)) && a13(i)>1.1*(c5(j))
j=i;
boom=false;
break;
end
end
if(boom==true)
Ylast=a13(j+14);
Xlast=New_Date(j+14);
figure (1)
plot(Xfirst,Yfirst)
hold on
plot(Xlast,Ylast)
drawnow; % To force figure to update
pause(0.2); % to allow time for it to render
end
j=j+1;
end
drawnow updates figures and processes any pending callbacks. Use this command if you modify graphics objects and want to see the updates on the screen immediately.

The same script/funktion different time with MatLab

i am somekind of "advanced" beginner in programing. I couldn't find answer to my question, i found this, MATLAB Speed Difference in Identical Code, but it is not exactly what i'm looking for. I have the following problem, my script or function consists of couple of segments, when i run them manually with Run Section, i need something like 15 sec. including scroling and clicking. When i start them as one function i need 72 sec. The code is:
a=diff(Min_ZR);
b=zeros(38000000,1)*NaN;
i=1;
while i<length(a)
if a(i)==1;
b(i)=1;
i=i+1;
else
b(i)=0;
i=i+1;
end
end
b(i:end) = [];
%%
iL=2;
kL=2;
L1=zeros(38000000,1)*0;
while iL<length(test_L)
if test_L(iL)==1
L1(iL)=L1(kL-1)+1;
iL=iL+1;
kL=iL;
else
L1(iL)=L1(iL-1);
iL=iL+1;
end
end
L1(iL:end)=[];
%%
iR=2;
kR=2;
L2=zeros(38000000,1)*0;
while iR<length(test_R)
if test_R(iR)==1
L2(iR)=L2(kR-1)+1;
iR=iR+1;
kR=iR;
else
L2(iR)=L2(iR-1);
iR=iR+1;
end
end
L2(iR:end)=[];
%%
iZ=2;
kZ=2;
ZR=zeros(38000000,1)*0;
%while i<length(test_R)
while iZ<length(b)
if b(iZ)==1
ZR(iZ)=ZR(kZ-1)+1;
iZ=iZ+1;
kZ=iZ;
else
ZR(iZ)=ZR(iZ-1);
iZ=iZ+1;
end
end
ZR(iZ:end)=[];
%%
L1=L1/4;
L2=L2/4;
ZR=ZR/8*34/22;
i=1;
diff_L1=zeros(38000000,1)*NaN;
diff_L2=zeros(38000000,1)*NaN;
while i<=length(ZR)
diff_L1(i)=L1(i)-ZR(i);
diff_L2(i)=L2(i)-ZR(i);
i=i+1;
end
diff_L1(i:end)=[];
diff_L2(i:end)=[];
l=length(ZR);
Drehmoment=zeros(l,1);
Drehmoment(1:10000)=100;
Drehmoment(10001:end)=380;
%%
figure % new figure
[hAx,hLine1,hLine2] = plotyy(ZR,diff_L2,ZR,Drehmoment);
title('Zahnradwandern 30.03.2016')
xlabel('Gesamtumdrehungen /1 ')
ylabel(hAx(1),'Differenzumdrehungen /1 ') % left y-axis
ylabel(hAx(2),'Drehmoment / Nm') % right y-axis
set(hAx(1),'Xlim',[0 1050000])
set(hAx(2),'Xlim',[0 1050000])
set(hAx(1),'Xtick',0:50000:1050000)
set(hAx(1),'XTickLabel',{'0','','100000','','200000','','300000','','400000','','500000','','600000','','700000','','800000','','900000','','1000000',''})
set(hAx(1),'Ylim',[-2 38])
set(hAx(1),'YTick',-2:2:38)
set(hAx(2),'YTick',0:20:400)
set(hAx(2),'Ylim',[0 400])
grid on
hold on
plot (ZR,diff_L1,'color','g')
hold off
legend('Differenzumdrehungen PL2','Differenzumdrehungen PL1','Antriebsmoment')
For each section i need less then 1 or 2 sec. just for the Plot i need a bit more, but as i wrote 15 sec. tops it is nowhere near to 72 sec. that i get when i Run the Function.
Thank you in advance.
I've found the Problem, if i start the program with run and time, it tells me 72 sec, or after i used hint 4. from Crowley 60 sec. It also says that the "Run and Time fkt" don't use resources but if i just Run the program it completes in under 10sec.
Does your code improve it's performance when:
you change indexes i to ii. i and j are reserved for imaginary units;
define b=nan(length(a),1); and L1=zeros(length(test_L),1); and ommit b(:,end)=[]; and L1(iL:end)=[];;
use for ii=1:length(a) instead of while loop;
use b=zeros(length(a),1);b(a==1)=1; instead of whole while loop;
use diff_L=L1-R1 instead of while loop;
use Drehmoment=ones(l,1)*380;Drehmoment(1:100000)=100;?
These are just suggestions where you can loose your time...
EDIT regarding to hbadert's comment
Have you tried:
Block=1;
BlockTic(Block)=tic;
%%Block 1
BlockToc(Block)=toc;
Block=Block+1;
BlockTic(Block)=tic;
%%...
BlockToc(Block)=toc;
AllTime=BlockToc(end)-BlockTic(1);
Times=BlockToc-BlockTic;
disp(['Global time: ',num2str(AllTime)])
disp('Block times:')
disp(Times')

Random number generator in Matlab

I am generating random sequences of numbers using the same seed:
sprev = rng(2,'v5uniform');
for i=1:N
%do some operations
rndIDX = randperm(sampleSize) ;
newdata= data(rndIDX(1:newSampleSize), :) ;
if x>y
remove=x1; %line 7
end
for l=1:M
%do something else
if l>xy & ~empty(remove)
%do something related to remove
elseif l>xy
%do nothing
else
%do something not related to remove
end
%more code here
end
end
However, when I comment out line 7, rndIDX returns a different sequence of numbers which for me is unexpected. There might be a bug somewhere in the code but I am not sure what is the relationship between the sequence produced by randperm and the code that follows. Also, if i keep the code as presented I always get the same newdata which is the expected behaviour. I just want to comment out line 7 and still get the same newdata.I can confirm that sampleSize is always the same as well as newSampleSize for both cases.

Matlab takes only 4 values of a digit for calculation

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

Document Clustering in Matlab

I am working on a code for document clustering in matlab. My document is :
'The first step in analyzing the requirements is to construct an object model.
It describes real world object classes and their relationships to each other.
Information for the object model comes from the problem statement, expert knowledge of the application domain, and general knowledge of the real world.
Britvic plc is one of the leading soft drinks manufacturers of soft drinks in the Beverages Sector functioning in Europe with its distribution branches in Great Britain, Ireland and France. '
As seen, the paragraphs contain different classes of data. The following is my main program:
global n;
n=1;
file1=fopen('doc1.txt','r');
%file 1 is now open
%read data from file 1
text=fileread('doc1.txt');
i=0;
%now text1 has the content of doc1 as a string.Next split the sentences
%into words.For that we are calling the split function
[C1,C2]=clustering(text)
And below comes the code for 'clustering':
function [C1,C2]=clustering(text)
global C1;
text1=strsplit(text,'.');
[rt1,ct1]=size(text1);
for i=1:(ct1-1)
var=text1{i};
vv=strsplit(var,' ');
text2=setdiff(vv,{'this','you','is','an','with','as','well','like','and','to','it','on','off','of','in','mine','your','yours','these','this','will','would','shall','should','or','a','about','all','also','am','are','but','of','for','by','my','did','do','her','his','the','him','she','he','they','that','when','we','us','not','them','if','in','just','may','not'},'stable');
[rt2,ct2]=size(text2);
for r=1:ct2
tmar=porterStemmer(text2{r});
mapr{i,r}=tmar;
end
end
[mr,mc]=size(mapr);
mapr
A=zeros(mr,mr);
for i=1:mr
for j=1:mc
for m=i+1:mr
for k=1:mc
if ~isempty(mapr{i,j})
%if(~(mapr{i,j}=='[]'))
%mapr(i,j)
if strcmp(mapr{i,j},mapr{m,k})
p=mapr{i,j};
str=sprintf('Sentences %d and %d match',i,m)
str;
str1=sprintf('And the word is : %s ',p)
str1;
A(i,m)=1;
A(m,i)=1;
end
end
end
end
end
end
sprintf('Adjacency matrix is:')
A
sprintf('The corresponding diagonnal matrix is:')
[ar,ac]=size(A);
for i=1:ar
B(i)=0;
for j=1:ac
B(i)=B(i)+A(i,j);
end
end
[br,bc]=size(B);
D=zeros(bc,bc);
for i=1:bc
D(i,i)=B(i);
end
D
sprintf('The similarity matrix is:')
C=D-A
[V,D]=eig(C,'nobalance')
F=inv(V);
V*D*F
%mvar =no of edges/total degree of vertices
no_of_edges=0;
for i=1:ar
for j=1:ac
if(i<=j)
no_of_edges=no_of_edges+A(i,j);
end
end
end
no_of_edges;
tdv=0;
for i=1:bc
tdv=tdv+B(i);
end
tdv;
mvar=no_of_edges/tdv
[dr,dc]=size(D);
temp=abs(D(1,1)-mvar);
x=D(1,1);
for i=2:dc
temp2=abs(D(i,i)-mvar);
if temp>temp2
temp=temp2;
x=D(i,i);
q=i
end
end
x
[vr,vc]=size(V);
for i=1:vr
V(i,q);
Track(i)=V(i,q);
end
sprintf('Eigen vectors corresponding to the closest value:')
Track
j=1;
m=1;
C1=' ';
C2=' ';
for i=1:vr
if(Track(i)<0)
C1=strcat(C1,text1{1,i},'.');
else
C2=strcat(C2,text1{1,i},'.');
end
end
I could generate the initial two clusters from the document. But then again, I want the clustering process to continue on the generated clusters producing more and more subclusters of each untill there is no change in the population generated. Can somebody help me implement a solution to this so that I can not only generate the clusters but also to keep track of them for further processing. Thanks in advance.