Creating two vectors from one.

1 Ansicht (letzte 30 Tage)
Giuseppe
Giuseppe am 26 Mär. 2014
Kommentiert: Giuseppe am 26 Mär. 2014
Hi I have a vector;
x = [2 4;3 -8; 1 2; 2 4;]
I need to produce two vectors like the ones to the left and right below. To get the vector on the right it is x_r = [2*-8 3*2 1*4] and same process for left(x_l) the arrows show what needs to be multiplied by what.
If the number of rows (columns will always = two) increase or decrease or the numbers change I need to account for this. Is there a way where I can do this by vectorization or will I be stuck with loops.
Thanks,

Akzeptierte Antwort

Mischa Kim
Mischa Kim am 26 Mär. 2014
Bearbeitet: Mischa Kim am 26 Mär. 2014
Giuseppe, use
xr = x(1:end-1,1).*x(2:end,2)
xr =
-16
6
4
and equivalent for the other calculation.
  1 Kommentar
Giuseppe
Giuseppe am 26 Mär. 2014
Bearbeitet: Giuseppe am 26 Mär. 2014
Thank you Mischa. You are a legend!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Chandrasekhar
Chandrasekhar am 26 Mär. 2014
x = [2 4;3 -8; 1 2; 2 4];
[m,n]=size(x);
for i= 1:m-1
a(i,1) = x(i,2)*x(i+1,1);
a(i,2) = x(i,1)*x(i+1,2);
end
  1 Kommentar
Giuseppe
Giuseppe am 26 Mär. 2014
Thanks for the contribution. I really appreciate it.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots 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