Stuck in Indexing of a Matrix(or Cell Array)

조회 수: 2 (최근 30일)
pradeep kumar
pradeep kumar 2015년 3월 25일
댓글: pradeep kumar 2015년 3월 26일
Hi all,I have just started learning MATLAB . Please find my codes below
m= ['A','B','C'];
cs=size(m,2);
for i=1:cs
for j=1:cs
if i~=j
s1=(m(i));s2=',';s3=(m(j));
s=strcat(s1,s2,s3);
disp(s);
end
end
end
It produces the following output on command window.
* A,B
* A,C
* B,A
* B,C
* C,A
* C,B
But , i want to wrap up all the outputs into a single matrix (or Cell Array ) , Lets say new_M . So that the values of new_M shall contain all the above values like this .
new_M (6,1) =
[ A,B
A,C
B,A
B,C
C,A
C,B ]
Your help will be highly appreciatated . Thanks in advance.

채택된 답변

James Tursa
James Tursa 2015년 3월 25일
편집: James Tursa 2015년 3월 25일
E.g., using the cell array approach:
m= ['A','B','C'];
cs=size(m,2);
new_M = cell(cs*(cs-1),1); % Pre-allocate your cell array
k = 0; % Initialize counter for cell array
for i=1:cs
for j=1:cs
if i~=j
s1=(m(i));s2=',';s3=(m(j));
s=strcat(s1,s2,s3);
disp(s);
k = k + 1; % Increment cell array counter
new_M(k) = {s}; % Stuff string into cell array element
end
end
end
  댓글 수: 1
pradeep kumar
pradeep kumar 2015년 3월 26일
Thank a TON Mr James Tursa . That tiny logic was not clicking in my mind from long time . You saved me .

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by