How can I add new data to specific row and colum of a matrix

Eg: A = [1 2 5; 3 4 6; 7 8 9];
i want to make it
A = [1 0 2 5; 3 0 4 6; 0 0 0 0; 7 0 8 9];
Thank you

 Accepted Answer

EDIT
B= [1 2 5; 3 4 6; 7 8 9]
n1=2 %Position of the row to add
m1=3 %position of the column to add
%-------------------------
A=zeros(size(B)+2)
A(2:end-1,2:end-1)=B
[n,m]=size(A)
A=[A(:,1:m1) zeros(n,1) A(:,m1+1:end)]
A=[A(1:n1,:); zeros(1,m+1) ;A(n1+1:end,:)]
A([1 end],:)=[];
A(:,[1 end])=[]

More Answers (1)

A = [1 2 5; 3 4 6; 7 8 9]
B = zeros(size(A) + 1)
B([1 1 0 1]>0,[1 0 1 1]>0) = A
OR
A = reshape(1:9,3,[])';
B = zeros(size(A)+1);
m = 3;
n = 2;
ii = ones(size(B));
ii(m,n) = 0;
B((+all(ii,2))*(+all(ii)) > 0) = A;
or just
B(all(ii,2),all(ii)) = A;

Categories

Tags

Community Treasure Hunt

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

Start Hunting!