HOW TO COMBINE TWO CELLS INTO ONE?

7 vues (au cours des 30 derniers jours)
Gili Kurtser
Gili Kurtser le 18 Oct 2019
Modifié(e) : dpb le 18 Oct 2019
Hello everyone
I have to arrays witch I want to combine:
M=[ 1 2 4 7 8 9].';
Y=[2018 2018 2018 2018 2018].';
and I want to get
[1-2018
4-2018
...] and so
how can I do this?
Tnx
  1 commentaire
Adam
Adam le 18 Oct 2019
They don't look like cell arrays, just numeric arrays.
Also
1-2018
is not a valid value to have in a numeric array.
You can concatenate two column vectors easily as
[M, Y]
if they are the same length, but you'll still have two columns. You'd have to format them into a string if you literally want '1-2018'

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 18 Oct 2019
Modifié(e) : dpb le 18 Oct 2019
As Adam notes, you have two arrays as shown (I took the liberty to reformat the original Q? to make more legible) and there's a mismatch in lengths so can't directly...that's probably just a typo in the posting one presumes.
Given they're month and year values, I'd suggest to convert to datetime instead; you can get the display format to be whatever you wish and do all kinds of magic with the values once have done...
t=datetime(Y,M,1,'Format','M-yyyy');
Example:
>> datetime(2018,[1;4],1,'Format','M-yyyy')
ans =
2×1 datetime array
1-2018
4-2018
>>

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by