Create an alternating matrix
조회 수: 5(최근 30일)
표시 이전 댓글
Hello. I am looking to create a 1xn matrix where I would enter a max value and a step size for the vector where each value alternates sign. For example I would enter a max value of 20 and a step size of 5 and the result would produce the following matrix;
[0 5 -5 10 -10 15 -15 20 -20]
Thank you in advance.
댓글 수: 0
채택된 답변
추가 답변(1개)
VBBV
2022년 11월 30일
maxV = 20;
stepS = 5;
I = zeros(1,2*(maxV/stepS)+1);
I1 = 0:stepS:maxV;
I2 = -I1(2:end);
I(1:2:end) = I1;
I(2:2:end) = I2;
I
참고 항목
범주
Find more on Operating on Diagonal Matrices 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!