Filter löschen
Filter löschen

join char/cell to double matrix

1 Ansicht (letzte 30 Tage)
fede
fede am 21 Sep. 2015
Bearbeitet: Image Analyst am 21 Sep. 2015
I have
c=['IBM';'SPY';'IVV'];
celldata=cellstr(c);
and
price= hist_stock_data(celldata');
I want a matrix as the following:
IBM SPY IVV
price1 price2 price3

Antworten (1)

Image Analyst
Image Analyst am 21 Sep. 2015
How about constructing a cell array
for col = 1 : size(c, 1)
% For each row in c
% Extract row from character array and place into a cell.
ca{col, 1} = c(col, :); % String goes into first row of cell array.
% Stuff number into second row of this column:
ca{col, 2} = price(col);
end
Or you could use a table variable instead of a cell array.
  2 Kommentare
fede
fede am 21 Sep. 2015
yes but the size of prices is 840,3, and not 1,3
Image Analyst
Image Analyst am 21 Sep. 2015
Bearbeitet: Image Analyst am 21 Sep. 2015
Looks like you forgot to mention that at first so there's no way I could have known. So just add a loop to add rows
for col = 1 : size(c, 1)
% For each row in c
% Extract row from character array and place into a cell.
ca{col, 1} = c(col, :); % String goes into first row of cell array.
% Stuff number into rows of this column:
for row = 1 : size(price, 1)
ca{col, row+1} = price(row, col);
end
end
Since you're not yet familiar with for loops and the size function (or else you would not have asked me), you should probably look up information on the for loop and the size function in the help documentation, since it's pretty basic yet crucial to know.
Actually, you'd better study up on cell arrays in the FAQ also: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F because they're far trickier than for loops.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by