Insert a blank column every n columns

Hi. I have a matrix (size 10x360) and I want insert a blank column every n columns. How can I do? I have tried to use for loop, but unsuccessfully.
Thanks, Alessandro

回答 (5 件)

Carlos
Carlos 2013 年 3 月 21 日
編集済み: Carlos 2013 年 3 月 21 日

0 投票

Try this, if you want to insert a blank column every 5 columns
A=rand(10,360);
A=num2cell(A);
A(:,1:5:end)=cell(1);

2 件のコメント

Alessandro Innocenti
Alessandro Innocenti 2013 年 3 月 21 日
It doesn't work...it deletes the column, it does not insert a blank column.
Carlos
Carlos 2013 年 3 月 21 日
OK, I misunderstood your question, look at the file exchange function insertrows uploaded by Jos, that http://www.mathworks.com/matlabcentral/fileexchange/9984, that may do the job for you.

サインインしてコメントする。

Peter
Peter 2013 年 3 月 21 日

0 投票

To insert columns with zeros you can try this:
A=rand(10,360);
n = 5;
nc = floor(360/n);
for c=1:nc;
B(:,(n*c+c-n):(n*c+c-1)) = A(:,(n*(c-1)+1):n*c);
end
If you need empty cells, you can convert your matrix to a cell array like Carlos showed before.
Andrei Bobrov
Andrei Bobrov 2013 年 3 月 21 日
編集済み: Andrei Bobrov 2013 年 3 月 22 日

0 投票

yourmatrix = randi(23,10,360);
n = 6;
yourmatrix(:,n:n:end) = nan;
or
a = randi(23,10,360);% your matrix
n = 5;
s = size(a);
k = floor(s(2)/n);
n1 = k*n;
out = [reshape([reshape(a(:,1:n1),s(1),k,[]),...
repmat(nan,[s(1),1,n])],s(1),[]),a(:,n1+1:end)];
or
a = randi(32,10,360);
n = 7;
out = zeros(s+[0 floor(s(2)/n)]);
out(:,n+1:n+1:end) = nan;
out(~isnan(out)) = a;
dinesh wankhede
dinesh wankhede 2017 年 2 月 18 日

0 投票

i have matrix a with some rows and some columns i want matrix b with same number of rows but double the number of columns such that every column repeats itself
minhua su
minhua su 2019 年 4 月 18 日

0 投票

this is how i solved the problem recently
str = strings([1,508*7])
i=[]
j=1
for i=1:508
str(j)=bamfiles(i)
j=j+7
end

1 件のコメント

minhua su
minhua su 2019 年 4 月 18 日
bamfiles is my original matrix, which is (size 1X508), and i create a new matrix str (the size is decided by how many every columns you want to insert)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2013 年 3 月 21 日

コメント済み:

2019 年 4 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by