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,

 채택된 답변

Mischa Kim
Mischa Kim 2014년 3월 26일
편집: Mischa Kim 2014년 3월 26일

1 개 추천

Giuseppe, use
xr = x(1:end-1,1).*x(2:end,2)
xr =
-16
6
4
and equivalent for the other calculation.

댓글 수: 1

Giuseppe
Giuseppe 2014년 3월 26일
편집: Giuseppe 2014년 3월 26일
Thank you Mischa. You are a legend!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chandrasekhar
Chandrasekhar 2014년 3월 26일

0 개 추천

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

Giuseppe
Giuseppe 2014년 3월 26일
Thanks for the contribution. I really appreciate it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2014년 3월 26일

댓글:

2014년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by