repeating a vector with increments

2 Ansichten (letzte 30 Tage)
Jeong Ho
Jeong Ho am 20 Feb. 2015
Beantwortet: arich82 am 20 Feb. 2015
Dear Matlab Central users, Hi, I have a row vector I and I want to create a row vector [I I+n I+2*n ... I+(N-1)*n] where N>1 and n are positive integers. For example, if I = [2 3] and N = n = 3, [2 3 5 6 6 9] Is there a trick to doing this efficiently? Thank you all!
Best, John

Antworten (2)

Star Strider
Star Strider am 20 Feb. 2015
The output vector you listed does not seem to me to correspond to the rule for it you posted. Please clarify.
This otherwise seems to work:
N = 3;
I = [2 3];
Q = (cumsum(ones(N,2))-1)*N;
Ym = bsxfun(@plus, I, Q);
Yv = reshape(Ym', 1, []); % Output
produces:
Yv =
2 3 5 6 8 9

arich82
arich82 am 20 Feb. 2015
Assuming you have a typo in your example and your desired output is actually
[2, 3, 5, 8, 9]
(i.e. an 8 instead of the repeated 6), then I think this should do what you want:
n = 3;
N = 3;
a = [2, 3];
b = n*[0:N-1];
c = bsxfun(@plus, a.', b);
c = c(:).'
or as a messy one-liner:
reshape(bsxfun(@plus, [2, 3].', 3*(0:3-1)), 1, []);
No gurantees that someone won't have a more clever solution, but if this doesn't give the desired result, perhaps a little more clarification would be helpful.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by