Path: news.mathworks.com!newsfeed-00.mathworks.com!solaris.cc.vt.edu!news.vt.edu!news.glorb.com!news2.glorb.com!tr22g12.aset.psu.edu!news.mathforum.org!not-for-mail
From: fckool <xtrangekid@sapo.pt>
Newsgroups: comp.soft-sys.matlab
Subject: optimize code
Date: Mon, 03 May 2010 13:03:59 EDT
Organization: The Math Forum
Lines: 41
Message-ID: <1976149983.64477.1272906270072.JavaMail.root@gallium.mathforum.org>
NNTP-Posting-Host: gallium.mathforum.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: support1.mathforum.org 1272906270 12213 144.118.94.39 (3 May 2010 17:04:30 GMT)
X-Complaints-To: news@news.mathforum.org
NNTP-Posting-Date: Mon, 3 May 2010 17:04:30 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:632082

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