Create a vector with known indices and assign to that values from known vectors

45 views (last 30 days)
Hello all,
I've got three vectors, with size 1x5
var1 = [ 5 6 7 8 9]
var2 = [11 12 13 14 15]
var3 = [17 18 19 20 21]
and I've got another vector with my indices, called positions, where
positions = [1 1 3 2 2]' (size 5 x 1)
*************
At the end I want to create a vector x, which will have the index of the positions column vector, but as we go along and fill in its values, it will get the nth value of the corresponding, var1 or var2 or var3. For example the 1st value of the x vector is corresponding to position 1, so we go to var1 and the 1st value is selected (5). For the last value (5th value) of the x vector, that corresponds to position 2, we go to var2 and the 5th value of var 2 (15) is selected
***************
The final vector x should be in that case x =[ 5 6 19 14 15]
Any ideas?
  1 Comment
Christina
Christina on 26 May 2015
In case my variables are in a timeseries format (say 4 timeseries), not a vector anymore,
for example
var1 = [5 6 7 8 9;50 60 70 80 90; 500 600 700 800 900;5000 6000 7000 8000 9000]
var2 = [11 12 13 14 15; 110 120 130 140 150; 1100 1200 1300 1400 1500; 11000 12000 13000 14000 15000]
var3 = [17 18 19 20 21; 170 180 190 200 210; 1700 1800 1900 2000 2100; 17000 18000 19000 20000 21000]
how can I again when the positions vector is [1 1 3 2 2 ]
obtain a final matrix x1, with the same logic as above, where it would have the format
x1 = [5 6 19 14 15; 50 60 190 140 150; 500 600 1900 1400 1500;5000 6000 19000 14000 15000]
??

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 May 2015
Try this:
var1 = [ 5 6 7 8 9]
var2 = [11 12 13 14 15]
var3 = [17 18 19 20 21]
positions = [1 1 3 2 2]
numItems = length(positions);
x = zeros(1, numItems);
for k = 1 : numItems
index = positions(k)
if index == 1
x(k) = var1(k);
elseif index == 2
x(k) = var2(k);
else
x(k) = var3(k);
end
end
% Print to command window:
x

More Answers (3)

Ingrid
Ingrid on 26 May 2015
maybe you can try something like this to keep it vectorized, if in reality your vector only has five elements, than you could also easily use a for loop but I am assuming you are just giving a small test example here
var1Temp = ones(5,1);
var1Temp(positions == 1) = var1(positions == 1);
var2Temp = ones(5,1);
var2Temp(positions == 2) = var2(positions == 2);
var3Temp = ones(5,1);
var3Temp(positions == 3) = var3(positions == 3);
x= var1Temp.*var2Temp.*var3Temp;
  1 Comment
Christina
Christina on 26 May 2015
Thanks a lot. You're right, in reality I've got 1000 elements, not 5, as well as 20 variables, not only 3.

Sign in to comment.


Thorsten
Thorsten on 26 May 2015
var = [var1; var2; var3]';
ind = (positions - 1)*size(var, 1) + [1:numel(positions)];
final = var(ind)

Andrei Bobrov
Andrei Bobrov on 26 May 2015
var = [ 5 6 7 8 9;11 12 13 14 15;17 18 19 20 21];
pts = [1 1 3 2 2];
[m,n] = size(var);
out = var(sub2ind([m,n],pts,1:n));

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!