Sample and save data to matrix

I have a matrix A=[x;y]', where x and y are vectors say
x=0.01:0.05:0.5
y=2:2:20
I would like to sample and save the data to a matrix Z as:
Z= [0.0600 4.0000
0.1600 8.0000
0.2600 12.0000
0.3600 16.0000
0.4600 20.0000]
Thank you

 採用された回答

Star Strider
Star Strider 2016 年 6 月 29 日

0 投票

This works:
x=0.01:0.05:0.5;
y=2:2:20;
z = [x(2:2:end)' y(2:2:end)']

2 件のコメント

Bobo
Bobo 2016 年 6 月 30 日
編集済み: Bobo 2016 年 6 月 30 日
Thanks Strider!
But what if the data is not equally spaced?
Star Strider
Star Strider 2016 年 6 月 30 日
I’m not certain what you mean by ‘not equally spaced’. If they’re not the same lengths, the code changes to:
x=0.01:0.05:0.5;
y=2:2:20;
minlen = min([length(x) length(y)]);
z = [x(2:2:minlen)' y(2:2:minlen)'];
This of course assumes that ‘x’ and ‘y’ are vectors. If they are matrices, you need to use the size function instead of length, specifying the dimension you want to use to specify the length, 1 for row length and 2 for column length.
If you want to choose a different step (here the step is 2), you would have to make the appropriate changes to the code to be sure the vectors are equal length, or use a cell array. The cell array would likely be easier.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2016 年 6 月 29 日

コメント済み:

2016 年 6 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by