join char/cell to double matrix
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!