Dear friend,
How to achieve the goal of multiply two cell array using MATLAB like this?
A={1,2,3},B={'a','b','c'}
to achieve A*B={{1,'a'},{1,'b'},{1,'c'},{2,'a'},{2,'b'},{2,'c'},{3,'a'},{3,'b'},{3,'c'},}
Your help would be highly appreciated!

 채택된 답변

Florian Bidaud
Florian Bidaud 2022년 10월 19일

0 개 추천

Hi,
C = {};
A={1,2,3};
B={'a','b','c'};
for i = 1:length(A)
for j = 1:length(B)
C{end+1} = {A{i},B{j}};
end
end

댓글 수: 5

Daniel Niu
Daniel Niu 2022년 10월 19일
Thank you Florian. It works.
Florian Bidaud
Florian Bidaud 2022년 10월 19일
you're welcome, if you're happy with the answer, please accept the answer
Daniel Niu
Daniel Niu 2022년 10월 19일
Dear Florian,
how to display the cell array like this {{1,'a'},{1,'b'},{1,'c'},{2,'a'},{2,'b'},{2,'c'},{3,'a'},{3,'b'},{3,'c'},}
I use celldisp, but it shows like this:
Thank you!
celldisp(C)
C{1}{1} =
1
C{1}{2} =
a
C{2}{1} =
1
If you type disp(C) you will get :
>> disp(C)
Columns 1 through 7
{1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell}
Columns 8 through 9
{1×2 cell} {1×2 cell}
if you type
>> for i = 1:length(C)
disp(C{i})
end
{[1]} {'a'}
{[1]} {'b'}
{[1]} {'c'}
{[2]} {'a'}
{[2]} {'b'}
{[2]} {'c'}
{[3]} {'a'}
{[3]} {'b'}
{[3]} {'c'}
You can also type :
>> disp([C{1,1:end}])
Columns 1 through 12
{[1]} {'a'} {[1]} {'b'} {[1]} {'c'} {[2]} {'a'} {[2]} {'b'} {[2]} {'c'}
Columns 13 through 18
{[3]} {'a'} {[3]} {'b'} {[3]} {'c'}
Daniel Niu
Daniel Niu 2022년 10월 19일
Thank you for the elaborate explanation!

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

추가 답변 (0개)

카테고리

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

질문:

2022년 10월 19일

댓글:

2022년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by