problem of printing in window command

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;

3 件のコメント

huda nawaf
huda nawaf 2012 年 5 月 7 日
please anyone reduce for loop for this code
i will be grateful
thanks in advance
TAB
TAB 2012 年 5 月 7 日
Code is not formated properly.
Jan
Jan 2012 年 5 月 7 日
@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.

回答 (1 件)

Jan
Jan 2012 年 5 月 7 日

1 投票

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.

9 件のコメント

huda nawaf
huda nawaf 2012 年 5 月 7 日
sorry for that, I do not u think that I do not respect the rules.
but sometime , i do not fully understand what the others mean when write comments. My native language is not Eglish.
regarding formatting of my code, I try to make my code be shorter then it is appearing as u see.
when display long code , no one read it.
im sorry again, I fully respect this forum and it's rules , because I benefited too much from this forum to solve many problems.
this format code
Nofusers=17856;k1=1;
f1=fopen('d:\matlab11\bin\flixster_time\ratings_tf\len_flix1.txt');
c=fscanf(f1,'%d');fclose(f1)
ini=1;celcod=cell(1,17856);
f=fopen('d:\matlab11\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=5
cc=1;
for b1=3619
if b~=b1
match=2;mismatch=-1;align1=[];align2=[];x=celcod{b}; y=celcod{b1};
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
max_i=1;max_j=1;max_scor=1;score=0;
for i=2:length(y)
for j=2:length(x)
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
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
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;
if mat(i,j).scor > max
max_i=i; max_j=j;max_scor=mat(i,j).scor;
end;
end;
end
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);
[align1;align2];
end
scor1(b,b1)=score;
end%%%end if
end;
end;
toc;
Oleg Komarov
Oleg Komarov 2012 年 5 月 7 日
By formating the code we mean this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099
huda nawaf
huda nawaf 2012 年 5 月 7 日
thank I saw it, but in this link there are two chices.
either format code by {code} or leave blanks.
I did the second choice earlier.
or may mean I have to place some comments in my code , then it is easier to understand it.
but if u mean I have to use {code}, i do not see it now,so I have to make new post.right?
Jan
Jan 2012 年 5 月 7 日
If you mark a block of code with the mouse an press the "{}code" button, the forum interface inserts two spaces in front of each line. But you can insert the spaces manually also just by typing two spaces.
Lines starting with two spaces are displayed as code, e.g. with considering line breaks.
huda nawaf
huda nawaf 2012 年 5 月 7 日
ok, what i have to do now.
can I make new post?
Jan
Jan 2012 年 5 月 7 日
I'm not sure, if your question is serious. Duplicate posts for the same problem are not wanted in this and other forums.
You can edit your original question. Simply press the "edit" button. Now modify the code such that it 1. starts with at least 2 spaces in each line, 2. contains one command per line only, and 3. follows the smart indent rules of Matlab's editor - CTRL-A to mark all text and CTRL-I to indent it nicely.
huda nawaf
huda nawaf 2012 年 5 月 8 日
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
huda nawaf 2012 年 5 月 11 日
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
Jan 2012 年 5 月 11 日
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

この質問は閉じられています。

質問済み:

2012 年 5 月 7 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by