Using horzcat for string and regular matrix?

2 vues (au cours des 30 derniers jours)
AbuYusuf
AbuYusuf le 7 Juin 2017
Commenté : AbuYusuf le 7 Juin 2017
I have two matrices:
A={'ai' 'ba';'cr' 'dk'}
B=[1;2]
how can I put them together as following: C= [1 'ai' 'ba';2 'cr' 'dk']
I know this can be done using horzcat but for regular matrix. Is there a way to do it with two different matrix types like my case?
  1 commentaire
Stephen23
Stephen23 le 7 Juin 2017
Modifié(e) : Stephen23 le 7 Juin 2017
Your output example does not make much sense: MATLAB will implicitly convert the types, and because [] is a concatenation operator you will end up joining all of those strings parts into a single 2x6 matrix. This is unlikely to be what you intended. Perhaps you really want the output to be a simple 2x3 cell array:
C = {1,'ai','ba';2,'cr','dk'}

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 7 Juin 2017
>> A = {'ai','ba';'cr','dk'};
>> B = [1;2];
>> C = [num2cell(B),A]
  1 commentaire
AbuYusuf
AbuYusuf le 7 Juin 2017
Perfect! Thanks Stephen

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