join char/cell to double matrix

1 次查看(过去 30 天)
fede
fede 2015-9-21
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

回答(1 个)

Image Analyst
Image Analyst 2015-9-21
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 个评论
fede
fede 2015-9-21
yes but the size of prices is 840,3, and not 1,3
Image Analyst
Image Analyst 2015-9-21
编辑:Image Analyst 2015-9-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by