Generate a matrix with alternative positive and negative values with ones
Show older comments
Hello, Any idea how to generate a matrix with ones with positive and negative values. For example
We know,
A=ones(n,1)=[1;1;1;1], if n=4
I would like a matrix like this:
A=[1;-1;1;-1]
However, n will change of size depending on the processed data.
On the other hand, I would like to generate a matrix with the following form
if n=10
A=[1;0.5;0;-0.5;-1;-.5;0;0.5;1;0.5], the n value can change depending on the uploaded data.
Thank you.
Accepted Answer
More Answers (2)
n = 10;
vec = (0:n-1)';
%Array A
A = cospi(vec)
%Array B
vec0 = vec+4;
B = 4*abs(vec0/8-floor(vec0/8+0.5))-1
1 Comment
Jorge Luis Paredes Estacio
on 21 Jul 2023
Another approach:
r = 4;
c = 5;
A = (-1).^((1:r).' + (1:c))
1 Comment
Jorge Luis Paredes Estacio
on 21 Jul 2023
Categories
Find more on Operators and Elementary Operations 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!