Effacer les filtres
Effacer les filtres

How to multiply two cell array

1 vue (au cours des 30 derniers jours)
Daniel Niu
Daniel Niu le 19 Oct 2022
Commenté : Daniel Niu le 19 Oct 2022
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!

Réponse acceptée

Florian Bidaud
Florian Bidaud le 19 Oct 2022
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 commentaires
Florian Bidaud
Florian Bidaud le 19 Oct 2022
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 le 19 Oct 2022
Thank you for the elaborate explanation!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by