How to add a matrix data to cell array?
Afficher commentaires plus anciens
I have var1 in cell array and var2 in matrix.
var1 = {'a' 'b' 'c'}
var2 = ([1 2 3])'.
I like add var2(1) to var1{2,2}, var2(2) to var1{3,2} and var2(3) to var1{4,2}. How can I do that smartly, not one by one? Thank you!
4 commentaires
Guillaume
le 1 Nov 2019
What does it mean to you to add the character 'a' to the number 1?
Wes
le 1 Nov 2019
KALYAN ACHARJYA
le 1 Nov 2019
var1 = {'a' 'b' 'c'}
var2 = [1 2 3]';
What result you are expecting? Result=???
Wes
le 1 Nov 2019
Réponses (2)
Guillaume
le 1 Nov 2019
Probably the best answer is don't use a cell array, use a table which is designed exactly for the purpose of having named columns:
>> var1 = {'a', 'b', 'c'}
>> var2 = magic(3);
>> t = array2table(var2, 'VariableNames', var1)
t =
3×3 table
a b c
_ _ _
8 1 6
3 5 7
4 9 2
>> t.a %access data in column 'a'
ans =
8
3
4
1 commentaire
Wes
le 1 Nov 2019
the cyclist
le 1 Nov 2019
Modifié(e) : the cyclist
le 1 Nov 2019
var1 = {'a' 'b' 'c'};
var2 = [1 2 3]';
var1(2:4,2) = num2cell(var2)
1 commentaire
Wes
le 1 Nov 2019
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
