Hi, I am new to Matlab and I'm trying to use it as a tool to create a model.
I have two vectors, both of them are <1x6001> double,
x=(x1,x2,x3...)
y=(y1,y2,y3...)
but what I need is to merge them as one in the following way:
z=(x1,y1;x2,y2;x3,y3...,xn;yn) - comma separates the elements, and semicolon separates the pairs.
so that I can use it as input data.
Is there a way to do it?

 採用された回答

Daniel M
Daniel M 2019 年 10 月 29 日
編集済み: Daniel M 2019 年 10 月 29 日

1 投票

Simple:
x = 1:10;
y = x + 10; % for example
% x and y are both [1x10] row vectors as you mentioned
z = [x(:), y(:)];
% z is a [10x2] matrix of
% [x1, y1;
% x2, y2;
% .....
% xn, yn]
And if you want it back in 'row' form, just transpose z using an apostrophe '

7 件のコメント

Daniel M
Daniel M 2019 年 10 月 29 日
I definitely get a 10x2 doing the way I wrote it. I get a 1x20 using z = [x,y]. Clearly, your x and y are [10x1].
Stephan
Stephan 2019 年 10 月 29 日
Forget it - my fault, comment deleted ;-)
Lucas Stadnik
Lucas Stadnik 2019 年 10 月 29 日
Thanks for the help, but how can I save them using the comma and semicolon?
z=(x1,y1;x2,y2;x3,y3;...;xn,yn)
The data is too large so I can't really do it manually.
Thank yo again for your help and time.
Daniel M
Daniel M 2019 年 10 月 29 日
You don't need to.
xcol = x(:);
% this lists all the elements of x in one big column
% same thing as doing [x1;x2;...;xn]
ycol = y(:); % same thing
z = [x(:), y(:)];
% this concatenates x and y side by side
% same thing as [x1, y1 ; x2, y2; ...; xn, yn]
Lucas Stadnik
Lucas Stadnik 2019 年 10 月 29 日
Thanks, I understand it now.
Can I use the function "save" to save "z" as a .txt?
Actually I just need to copy z and paste it into another program.
Daniel M
Daniel M 2019 年 10 月 29 日
You can use writematrix to save it as a txt (among other similar things). save only save mat files.
Lucas Stadnik
Lucas Stadnik 2019 年 10 月 29 日
Thank you for your help and time.
I appreciate it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFunctions についてさらに検索

タグ

質問済み:

2019 年 10 月 29 日

コメント済み:

2019 年 10 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by