From: fckool on 3 May 2010 09:03 hi.. I have this code, and i want optimize it.. any suggestion? thank you in advance. clear all; File=input('Ficheiro: ','s'); fid=fopen(File,'rt'); %modo texto - elimina o CR à partida A=fread(fid);%,'ubit1'); Comp=length(A); Simb=[]; Ocorr=[]; for i=1:Comp %length(Ocorr)+1 if isempty(Simb) Simb(1)=A(i,1); Ocorr(1)=1; else j=length(Simb); while j~=0 if A(i,1)==Simb(j) break; end j=j-1; end if j==0 %length(Simb)+1; Simb(length(Simb)+1)=A(i,1); Ocorr(length(Ocorr)+1)=1; else Simb(j)=A(i,1); Ocorr(j)=Ocorr(j)+1; end end end Prob=Ocorr/sum(Ocorr); P=sum(Prob); %para efeitos de teste: tem de ser 1 Simb=Simb'; Simb=char(Simb); Simb=cellstr(Simb); Simb=Simb';
From: Roger Stafford on 3 May 2010 15:01 fckool <xtrangekid(a)sapo.pt> wrote in message <1976149983.64477.1272906270072.JavaMail.root(a)gallium.mathforum.org>... > hi.. I have this code, and i want optimize it.. any suggestion? > ....... I've done this in haste and haven't tried it out, but here is my idea. If it isn't right, something similar should work. [u,m,n] = unique(A,'first'); c = histc(A,u); [t,p] = sort(m); Simb = u(p); Ocorr = c(p); This just calculates 'Simb' and 'Ocorr'. The rest looks straightforward. Roger Stafford
From: fckool on 3 May 2010 11:36 wow... it seems work. Can you do a small explanation ? Thank you
From: Roger Stafford on 3 May 2010 16:14 fckool <xtrangekid(a)sapo.pt> wrote in message <845054490.65201.1272915427662.JavaMail.root(a)gallium.mathforum.org>... > wow... it seems work. > Can you do a small explanation ? > Thank you If I understand your code correctly, what it does is to list in 'Simb' the first appearances of unique elements in A in the order of those appearances. In 'Ocorr' is placed a count of the total number of appearances for each corresponding value in 'Simb'. However, as you apparently surmised, the algorithm is not very efficient. The calls "[u,m,n] = unique(A,'first')" and "c = histc(A,u)" do this in a more efficient manner, with the unique elements of A being placed in u and corresponding counts in c. However they are in sorted order of u elements instead of order of occurrence in A. Fortunately, the values in m give the indices for the first occurrences in A of the elements in u if the 'first' option is used. Therefore we must do a sort on m to find the necessary permutation, p, that needs to be performed on u and c elements to reorder them in the order of their original occurrences in A instead of their numerical order in u. Roger Stafford
From: fckool on 12 May 2010 17:38 hello Ok, i think i understood that. I have other question: who can i detect the ASCII code 10 (line-feed) and subtract it in 'Simb' and one value on 'Occur'? Thank you
|
Next
|
Last
Pages: 1 2 3 Prev: How to connect my USB camera with MATLAB and preview it? Next: How to use Contour Plot |