How to make alternating signs on a matrix?

7 views (last 30 days)
E=[1:4;6:9;11:14;16:19]
So I have this matrix, how can i make it so that the components come one with + sign and one with - sign, so alternating signs?
We just got started today with Matlab so sorry if it is a beginners question

Accepted Answer

Star Strider
Star Strider on 7 Oct 2020
Another approach:
E=[1:4;6:9;11:14;16:19];
m = (-1).^toeplitz((1:size(E,1)),(1:size(E,2))); % Multiplier Matrix
Out = E .* m
producing:
Out =
-1 2 -3 4
6 -7 8 -9
-11 12 -13 14
16 -17 18 -19
Create ‘Out’ by multiplying them in the same line if you want. (I kept them separate for clarity.)

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 7 Oct 2020
There are many ways. This is probably the easiest.
E(1:2:end) is the 1, 3, 5,... th element of the matrix
E(2:2:end) is the 2,4,6,...th element of the matrix, so
E(1:2:end)=-E(1:2:end)
or E(2:2:end)=-E(2:2:end)

Categories

Find more on Creating and Concatenating 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!