join char/cell to double matrix

3 ビュー (過去 30 日間)
fede
fede 2015 年 9 月 21 日
編集済み: Image Analyst 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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by