I have a matrix A = [1 1 0 0 1 1]

13 views (last 30 days)
Hamza Naeem
Hamza Naeem on 8 Feb 2020
Answered: Stephen23 on 9 Feb 2020
I have a matrix A = [1 1 0 0 1 1]. I want to add 0 one by one on this matrix like this
A = [0 1 1 0 0 1 1]
A= [1 0 1 0 0 1 1]
A = [1 1 0 0 0 1 1]
up to so on all the places. How I will do that via coding?

Answers (2)

KSSV
KSSV on 8 Feb 2020
Edited: KSSV on 8 Feb 2020
A = [1 1 0 0 1 1] ;
m = length(A) ;
B = zeros(m+1) ;
insert = @(a, x, n)cat(2, x(1:n), a, x(n+1:end));
for i = 1:m+1
T = insert(0,A,i-1) ;
B(i,:) = T ;
endfor

Stephen23
Stephen23 on 9 Feb 2020
>> A = [1,1,0,0,1,1];
>> B = toeplitz([0,A])
B =
0 1 1 0 0 1 1
1 0 1 1 0 0 1
1 1 0 1 1 0 0
0 1 1 0 1 1 0
0 0 1 1 0 1 1
1 0 0 1 1 0 1
1 1 0 0 1 1 0

Categories

Find more on Model Predictive Control Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!