Repmat a vector n times where n is not a whole number

6 ビュー (過去 30 日間)
Stef
Stef 2018 年 6 月 24 日
編集済み: Ankita Bansal 2018 年 6 月 25 日
I have a vector y consisting out of 10 elements. I want have a for loop, where I repmat the vector y to a new vector. However repmat is not working since n is not always a whole number. In this case I want it to choose the element which is next, e.g. if n = 4.2 it should take y 4 times and add element 1 and 2 separately. How can I do this?
y = [1:10];
for n = 4:0.5:6
Z = repmat(y,n,1);
end
  3 件のコメント
Stephen23
Stephen23 2018 年 6 月 25 日
編集済み: Stephen23 2018 年 6 月 25 日
Adam
Adam 2018 年 6 月 25 日
Also there were a couple of answers to essentially the same question here:

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

採用された回答

Stephen23
Stephen23 2018 年 6 月 25 日
編集済み: Stephen23 2018 年 6 月 25 日
To take the smaller index (equivalent to fix):
>> y = 1:10;
>> n = 4.2;
>> [repmat(y,1,fix(n)),y(1:numel(y)*mod(n,1))]
ans = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2
  5 件のコメント
Stephen23
Stephen23 2018 年 6 月 25 日
編集済み: Stephen23 2018 年 6 月 25 日
"could you please show me how to make the 12 times 3 matrix instead of a 4 times 9? I mean for a matrix of four rows that row 5 is the same as row 1"
I don't understand. My example shows a 4x3 matrix and n=2.5, which would give a 10x3 matrix. How do you expect to get a 12x3 matrix from that?
>> [repmat(y,fix(n),1);y(1:size(y,1)*mod(n,1),:)]
ans =
2 9 4
5 2 3
6 4 9
5 4 7
2 9 4
5 2 3
6 4 9
5 4 7
2 9 4
5 2 3
Stef
Stef 2018 年 6 月 25 日
Thank you very much, Stephen!

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

その他の回答 (1 件)

Ankita Bansal
Ankita Bansal 2018 年 6 月 25 日
編集済み: Ankita Bansal 2018 年 6 月 25 日
Hi Stef, are you saying that you have a 4x9 matrix A and you want to reshape it to 12x3? If so then you can use reshape function available in MATLAB.
But if you are saying that you want to create a 12x3 matrix B, where B(5,:) is equal to B(1,:) and B(6,:) is equal to B(2,:) and so on, then which elements from A do you want to drop?
Or are you saying that you want to create a 12x9 matrix from A? if so then also you can write
M= [repmat(y,fix(n),1);y(1:size(y,1)*mod(n,1),:)]

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by