Adding a column every nth column without replacing existing one

Hey all,
I am looking for a solution to the problem I mentioned in the title.
FOr example, I have matrix A
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
I now want to add every 2nd (and later 3th) column a new column containing zeros.
The result should be like this:
result = [1 0 2 0 3 0 4 0; 5 0 6 0 7 0 8 0; 9 0 10 0 11 0 12 0];
I know how to create a new column containg only zeros
B = zeros(number_of_rows,1);
Previously, I used
C = [A(:,1:2) B A(:,2+1:end)];
But this only works for a specific column and not every nth column.
Thanks for your answers in advance!

 채택된 답변

Adam Danz
Adam Danz 2019년 12월 11일
편집: Adam Danz 2019년 12월 11일
The variable n indicates every n-columns that should be 0s. It must be an integer greater than 1.
See inline comments for details.
% INPUTS
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
n = 2; % for n=2, every 2nd col will be 0s
% Determine the number of columns of resultant matrix
nCol = floor(size(A,2)/(n-1)*n);
% matrix of all 0s
result = zeros(size(A,1),nCol);
% Determine column indices of non-zeros
colIdx = 1:nCol;
colIdx(n:n:end) = [];
% Insert values from A into result matrix
result(:,colIdx) = A

댓글 수: 3

Here's the function-version of that (inspired by Max' answer).
function result = insertZeroColumns(A,n)
% A is a matrix
% n is a scalar that inserts a column of 0s into every n-th column
% Determine the number of columns of resultant matrix
nCol = floor(size(A,2)/(n-1)*n)
% matrix of all 0s
result = zeros(size(A,1),nCol);
% Determine column indices of non-zeros
colIdx = 1:nCol;
colIdx(n:n:end) = [];
% Insert values from A into result matrix
result(:,colIdx) = A;
end
Example
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
result = insertZeroColumns(A,2);
result = insertZeroColumns(A,3);
Thanks for your answer! I chose the non-function code because I wanted to implement it into an existing script with other things and realized, that in my version it is not possible to insert a function in a script.
Glad I could help out!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Max Murphy
Max Murphy 2019년 12월 11일
Test Data
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
Function
function A = addDataNthColumn(A,B,n)
% ADDDATANTHCOLUMN Add data in column vector or matrix B to every nth
% column of A
if nargin < 3
n = 3;
end
if size(A,1) ~= size(B,1)
error('A and B must have same number of rows');
end
if size(A,2) >= n
% Use recursion to iterate on A
A = [A(:,1:(n-1)), B, addDataNthColumn(A(:,n:end),B,n)];
else
A = [A, B]; % Last "set" just concatenate them
end
end
Usage
>> test = addDataNthColumn(A,zeros(3,2),2)
test =
1 0 0 2 0 0 3 0 0 4 0 0
5 0 0 6 0 0 7 0 0 8 0 0
9 0 0 10 0 0 11 0 0 12 0 0

댓글 수: 5

rowA=size(A,1);
colA=size(A,2);
B=zeros(rowA,colA*2);
B(:,1:2:2*colA)=A;
It is every 2th column.
Adam Danz
Adam Danz 2019년 12월 11일
편집: Adam Danz 2019년 12월 11일
Nice profile picture. Is that a well isolated neuron I see? :)
Nice profile picture. Is that a well isolated neuron I see? :)
Haha it's from a sparse extracellular array, so I don't know about well-isolated, but otherwise yes :)
Well there must have been good thresholding on your instrumentation, then. It's such a good feeling to work with an isolatable single neuron but mult-iunit activity is also fun. Now you have me all nostalgic.
Thanks for your answer!

댓글을 달려면 로그인하십시오.

Chuguang Pan
Chuguang Pan 2019년 12월 12일
It is every 2th column. Now I put it in Answer this question
rowA=size(A,1);
colA=size(A,2);
B=zeros(rowA,colA*2);
B(:,1:2:2*colA)=A;

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2015b

질문:

2019년 12월 11일

댓글:

2019년 12월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by