a problem about sprintf, array and concetnation

i need a hope about this code, there's a mistake but i just can't figured it out..
i need the result:
out2=
[SemenPadang vs persiwa, SemenPadang vs persipura, PSPS vs ... SemenPadang, bye, Persisam vs semen padang;
PSPS vs Persipura, PSPS vs Persiwa, PSPS vs SemenPadang, bye, ...
BontangFc vs PSPS ]
teams = {'SemenPadang','PSPS','Sriwijaya','Persija','PelitaJaya','Persib','Persijap','Persibo','Arema','Persema','Persela','Deltras','Persiba','PSM','BontangFC','Persisam','Persiwa','Persipura'};
out2 =
[17 18 -2 0 -3 -4 5 6 -7 -8 9 10 -11 -12 13 14 -15 -16;
18 17 1 0 -4 -3 6 5 -8 -7 10 9 -12 -11 14 13 -16 -15 ]
ab = cat(2, teams, teams);
[brs, klm]= size(out2);
for b2= 1:brs
for k2= 1:klm
if out2(b2, k2) > 0
out2(b2, k2)= sprintf('%s vs %s\n', ab{b2}, ab{abs(out2(b2, k2))}); % when out2 is positive,
elseif out2(b2, k2) < 0
out2(b2, k2)= sprintf('%s vs %s\n', ab{abs(out2(b2, k2))}, ab{b2}); % it work's on single elements
else
out2(b2, k2)= sprintf('bye');
end
end
end
thx for attention and guidance..

댓글 수: 2

Jan
Jan 2012년 4월 2일
Please format the code as explained in the "Markup help" link.
What exactly is the "mistake"? Is "[ semen padang vs ..." the wanted or the unwanted result and which format does this have? Is it a string?
mashudi
mashudi 2012년 4월 4일
thx simon,
pardon me, i try to concatenate between string value of "teams" through array of "out2" so i can make an array result's "out2", the final purpose was i can put the results's displayed on uitable..
since i have no another solution, i just put those operation 'coz it work's on a single element's..

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 4월 4일

0 개 추천

sprintf() results in strings, which are character arrays. You cannot store an entire character array into a single location of a numeric array such as out2. In order to store a character array into a location designated with just two subscripts, you have to be storing into a cell array, such as
out3{b2, k2} = sprintf('%s vs %s\n', ab{abs(out2(b2, k2))}, ab{b2});

댓글 수: 1

mashudi
mashudi 2012년 4월 4일
thx walter, it make another option for me, i'll tried it ..

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by