Azzera filtri
Azzera filtri

How to join two vectors

1 visualizzazione (ultimi 30 giorni)
Thales
Thales il 2 Mag 2014
Commentato: dpb il 2 Mag 2014
I have a vector x1 = [1; 0; -2] and an array R = [1 0; 2 -0.5; 6 0] and I want to create a vector Xg = [0 -0.5 1 0 -2 0].
The logic to create Xg is that it will be long enough to have all the elements in x1 and R and to find the elements in Xg, you first put the elements on the second column of R (in the lines specified by the first column of R) and, after putting all elements of R in Xg, complete Xg with the elements x1, in the order they appear.
Do you understand the problem? Any ideas how to do it?

Risposta accettata

dpb
dpb il 2 Mag 2014
>> x2(R(:,1))=R(:,2);
>> x2(~ismember([1:length(x2)],R(:,1)))=x1
x2 =
0 -0.5000 1.0000 0 -2.0000 0
  1 Commento
dpb
dpb il 2 Mag 2014
BTW, the above needs a preallocation to the maximum length or the second will truncate the former. Just so happens the '6' in the first data set predefines it with the first case to be the maximum needed.

Accedi per commentare.

Più risposte (1)

Roger Stafford
Roger Stafford il 2 Mag 2014
Xg(R(:,1)) = R(:,2);
Xg(setdiff((1:size(x1,1)+size(R,1))',R(:,1))) = x1;

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by