Asked by huda nawaf
on 7 May 2012

hi, i have code, i tried not print any result in command window because it is causing long time in running . but when run my code ,an result is appearing although I do not print any result. but I think it is because of using while.

this is what appear

ans= 1 1 how I can make this result do not appear in command window?

Also ,this code it has very long time in running,can replace some commands to be faster?

this is my code:

tic;

clear

Nofusers=17856;k1=1;

f1=fopen('d:\matlab\r2011a\bin\flixster_time\ratings_tf\len_flix1.txt');

c=fscanf(f1,'%d');

fclose(f1)

ini=1;

celcod=cell(1,17856);

f=fopen('d:\matlab\r2011a\bin\flixster_time\ratings_tf\flix_fina1.txt');

cod=fscanf(f,'%d');

for i=1:Nofusers

celcod{i}=cod(ini:c(i)+ini-1);

ini=ini+c(i);

end

gap = 0;

for b=1:Nofusers

cc=1;

for b1=1:Nofusers

if b~=b1

%the score of an insertion/deletion

match=2; mismatch=-1;

align1=[];

align2=[];

x=celcod{b};

y=celcod{b1};

%%%%%%initialization

x1(1:length(y),1:length(x))=0;

txt(1:length(y),1:length(x))=' ';

mat=struct('scor',x1,'pointer',txt);

for j=1:length(x)

mat(1,j).scor=x1(1,j);

mat(1,j).pointer='none';

end

for i=1:length(y)

mat(i,1).scor=x1(i,1);

mat(i,1).pointer='none';

end

%%%%fill

max_i=1;

max_j=1; max_scor=1; score=0;

for i=2:length(y)

for j=2:length(x)

%%%%%% calculate match score

letter1=x(j);

letter2=y(i);

if letter1==letter2

dig_scor=mat(i-1,j-1).scor+match;

else

dig_scor=mat(i-1,j-1).scor+mismatch;

end

%%%%% caculate gap score

up_scor=mat(i-1,j).scor+gap;

left_scor=mat(i,j-1).scor+gap;

%%%%%%%%%%%%%%%%%%%

if (dig_scor<=0 && up_scor<=0 && left_scor<=0) mat(i,j).scor = 0;

mat(i,j).pointer='none';

continue

end

%%%choose best score

if dig_scor>=up_scor

if dig_scor>=left_scor

mat(i,j).scor=dig_scor;

mat(i,j).pointer='diagonal';

else

mat(i,j).scor=left_scor;

mat(i,j).pointer='left';

end

else if up_scor>=left_scor

mat(i,j).scor=up_scor;

mat(i,j).pointer='up';

else

mat(i,j).scor=left_scor;

mat(i,j).pointer='left';

end

end

% % %%%% set maximum score

if mat(i,j).scor > max_scor

max_i=i;

max_j=j;

max_scor=mat(i,j).scor;

end

end

end % %%%trace back

max_scor;

j=max_j;

i=max_i;

while i>1 & j>1

if strcmp(mat(i,j).pointer,'none')==1

break;

end

if strcmp(num2str(mat(i,j).scor),'0')==1

break;

end

if strcmp(mat(i,j).pointer,'diagonal')==1

align1= [align1 {num2str(x(j))}];

align2=[align2 {num2str(y(i))}];

if strcmp(num2str(x(j)),num2str(y(i)))==1

score=score+2;

else

score=score-1;

end

i=i-1;

j=j-1;

elseif strcmp(mat(i,j).pointer,'left')==1;

align1= [align1 {num2str(x(j))}];

align2=[align2 '-'];

j=j-1;

score=score-1;

else

mat(i,j).pointer=='up'

align1= [align1 '-' ];

align2=[align2 {num2str(y(i))}];

i=i-1;

score=score-1;

end

end

align1=fliplr(align1);

align2=fliplr(align2);

scor1(b,b1)=score;

end%%%end if

end

end

fclose all

toc;

*No products are associated with this question.*

Answer by Jan Simon
on 7 May 2012

Some final comments:

- "mat(i,j).pointer=='up'" and "[align1;align2];" are either meaningless or even false
- "strcmp(num2str(x(j)),num2str(y(i)))==1" is most likely smarter, nicer and faster, when you write it as "x(j)==y(i)" or "isequal(x(j), y(i)" or "abs(x(j)-y(i)) < 10*eps" or what ever.
- In "if mat(i,j).scor > max" the term "max" is undefined - do not shadow the built-in function "max"!

And now I give up. You do not show effort to make answering your question as easy and efficient as possible. Then it is more efficient, if I spend my time for answering questions of other users, who show more respect for the forum rules.

Show 6 older comments

huda nawaf
on 8 May 2012

many thanks for these notes.

of curse, my question is serious. I'm phd student and this is my work.

i will follow all your notes

huda nawaf
on 11 May 2012

plesae , why this result ia apearing in command window:

ans:

1 1

I did not print any result , but this result appeared , I think it is the reason of long run time of my code.

please , what I must do to make this result not appear.

Jan Simon
on 11 May 2012

The line "mat(i,j).pointer=='up'" compares both characters of the string "mat(i,j).pointer" with the characters of 'up'. Ther sult is [TRUE TRUE], which is displayed as "1 1".

Perhaps you want:

if mat(i,j).pointer == 'up'

or

mat(i,j).pointer = 'up';

You can find the cause of such output either by checking the MLint warnings in editor, by stepping through the code line by line using the debugger or by checking the code manually.

Suggestions to improve the speed:

Pre-allocate!

"strcmp(a,b)" is enough, while "strcmp(a,b)==1" wastes time.

"if strcmp(num2str(mat(i,j).scor), '0') == 1" => num2str(mat(i,j).scor == 0

Opportunities for recent engineering grads.

## 3 Comments

## huda nawaf (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/37633#comment_77920

please anyone reduce for loop for this code

i will be grateful

thanks in advance

## TAB (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/37633#comment_77924

Code is not formated properly.

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/37633#comment_77937

@Huda, you ignore the repeated asking for formatting your code. I've spend enough time with formatting your former messages to improve the readability. It does not look like you appreciate the willingness of the contributors of this forum to assist you.

Please comment or accept the answer to your previous question, before you post a new one, which uses the answer posted there: http://www.mathworks.com/matlabcentral/answers/37608-need-shrter-code . Then others can see, that the problem is solved already and they can save the time for reading the question.