Creating a table from different sized vectors

115 次查看(过去 30 天)
I have a number of vectors (x1,y1,x2,y2, etc) with different lengts and I want to extract them to a .csv containing all these. Specifically,
  • 1st column = x1 (289 rows)
  • 2nd column = y1 (289 rows)
  • 3rd column = x2 (300 rows)
  • 4th column = y2 (300 rows)
I try to use the horzcat but it does not work due to different size. Any ideas or solutions?
  1 个评论
Stephen23
Stephen23 2015-12-17
Rather than storing these in numbered variables these vectors should really be stored in a cell array. Then the task becomes a trivial loop with indexing to allocate the vectors into a proallcoated array.
Numbered variables are invariably a bad idea:

请先登录,再进行评论。

采纳的回答

Renato Agurto
Renato Agurto 2015-12-17
Hi, you could try:
A = nan(no_of_columns, longest_array); % or zeros or whatever you want to fill the unused spaces
and then (or for example in a for loop):
A(1:length(x1),1) = x1;
A(1:length(y1),2) = y1;
...
I hope this is what you want
  1 个评论
ju.shu
ju.shu 2020-6-17
hey,
what if I have two columns each one of them 2xn (like 2x4012, and 2x9877) fo instance the columns represent points coordinates, how can I save the 2 columns in a table??
Thanks.

请先登录,再进行评论。

更多回答(3 个)

the cyclist
the cyclist 2015-12-17
Depends on what you want to appear in the "empty" spot. You could use NaN. Then
M = [[x1; nan], [y1; nan], x2, y2]
and then export the matrix M.

the cyclist
the cyclist 2015-12-17
One possibility is to store them in a cell array first, in which case empty will be empty.
M = cell(300,4);
M(1:299,1) = num2cell(x1);
M(1:299,2) = num2cell(y1);
M(:,3) = num2cell(x2);
M(:,4) = num2cell(y2);

Image Analyst
Image Analyst 2015-12-17
I'd just use fprintf() and write it out manually.

类别

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