I want to create a vector which runs from 1 to 260 with increments of 360 between every whole number.
I can do this manually by: y=linspace(1,2,360); y1=linspace(2,3,360);... and so on.
By combining these I would have a vector which was 260*360=93600 long. However, there must be a easier way of doing this? preferably without a loop.

 採用された回答

the cyclist
the cyclist 2012 年 1 月 18 日

1 投票

N = 260;
y = linspace(1,N,(N-1)*360+1);

6 件のコメント

Richard
Richard 2012 年 1 月 18 日
many thanks, I have slightly modified your answer to account for duplicates:
n=261;
linspace(1,n,(n-1)*360);
David Young
David Young 2012 年 1 月 18 日
So you don't want the increment to be 1/360?
the cyclist
the cyclist 2012 年 1 月 18 日
The change that you made will mean that the "fenceposts" will NOT exactly land on the integers. They will be off by increments of 1/(360*260). The +1 in my code was necessary to get the alignment correct. I suggest you get a feel for how this works by using smaller numbers than 260 and 360.
Richard
Richard 2012 年 1 月 18 日
I see what you mean, but the vector must be a length of 93600 as it is used to plot against another vector of that length.
David Young
David Young 2012 年 1 月 19 日
So maybe you need
linspace(1+1/360, n, (n-1)*360)
or
linspace(1, n-1/360, n, (n-1)*360)
Using linspace(1,n,(n-1)*360) is incorrect if the spacing between the points is meant to be 1/360. (See my answer below and the cyclist's comment above.)
Richard
Richard 2012 年 1 月 19 日
ok thanks for your help, worked great.

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

その他の回答 (1 件)

David Young
David Young 2012 年 1 月 18 日

1 投票

If you concatenate the vectors y, y1 etc. the integer elements will be repeats. That is, it will go [1 1.0028 ... 1.9972 2 2 2.0028 ... ] and likewise at 3, 4 etc. Is that what you want, or do you really want it to go [1 1.0028 ... 1.9972 2 2.0028 ...]?
Assuming you actually want a linear sequence all the way through, you can just use
yall = linspace(1, 260, 259*360+1);
My choice of the final argument makes the difference between successive entries equal to 1/360, which I think is what you mean by increments of 360 between each whole number. If you use 260*360 for N, this will not be the case.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by