Filter löschen
Filter löschen

Combining Vector in a matrix

2 Ansichten (letzte 30 Tage)
ammar ansari
ammar ansari am 26 Mai 2015
Bearbeitet: ammar ansari am 26 Mai 2015
I want to create a matrix that has first row as month second as date and the third as day
So far I am successful in creating three vectors and now I want to merge them in a matrix
function A=Jume2015
month = repmat('June', 30, 1);
date=[1:30];
d = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'};
dy = (d(mod(0:29, 7) + 1));
They should be merged in manner that I should get an output like this
>> m(11,:)
ans =
'June' [11] 'Thu'
Please help me.
If there are some errors in English please avoid them as English is a second language for me

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 26 Mai 2015
A cell array is very useful when you want to combine strings and numerical values into a single matrix. Let's build one:
MonthName = 'June' ;
DayNames = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'};
Ndays = 30 ;
M = cell(Ndays,3) ; % create a place holder
M(:,1) = {MonthName} % fill the first two with the same string
M(:,2) = num2cell(1:Ndays) % convert numbers to a cell array
M(:,3) = DayNames(1+mod(0:Ndays-1,7)) % math trick to index into DayNames

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 26 Mai 2015
function A=Jume2015
month = repmat('June', 30, 1);
date=[1:30];
d = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'};
dy = (d(mod(0:29, 7) + 1));
A = [cellstr(month),num2cell(date(:)),dy(:)];
  1 Kommentar
ammar ansari
ammar ansari am 26 Mai 2015
Bearbeitet: ammar ansari am 26 Mai 2015
Thanks thats a little bit close to what I was searching for but still it store dy as 1*1 cell rather then a string 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by