Welcome to MATLAB!
Matlab will allocate the memory for you on the fly, so this is a one line operation, no need for a loop :)
Use the colon to mark a continuous range of indices. For your question:
B = A(1,1:99).*A(1,2:100)
Note that the .* operator is used to mark element-wise multiplication, otherwise, you're trying to multiply [1x99] x [1x99], which isn't compatible and you'll receive an error. There are similar element-wise operations for exponentiation (.^) and division (./)
You can also go backwards, skip indices, and access a subset by using two colons for the range:
B = A(1, 1:99) .* A(1,100:-1:2)
B = A(1, 1:3:99) .* A(1,2:3:100)
B = A(1, [3,4,5]) .* A(1,[51,61,71])
B = A(1, 1:end-1) .* A(1, 2:end);
댓글 수: 0
댓글을 달려면 로그인하십시오.