13 views (last 30 days)

Show older comments

Hello

I have a matrix and I want to add new values on the rows which are the multiples of 3. Take the below as an example

A=[A(1); A(2);...;A(n)];

% I want to create Matrix B like below

B=[A(1);A(3);NaN;A(3);A(4);NaN;......;NaN;A(n-1);A(n)];

%how to creat matrix B

Thanks alot

Walter Roberson
on 31 Aug 2019

Should it be

B=[A(1);A(2);NaN;A(3);A(4);NaN;......;NaN;A(n-1);A(n)];

or

B=[A(1);A(2);NaN;A(2);A(3);NaN; A(3);A(4);NaN;......;NaN;A(n-1);A(n)];

Andrei Bobrov
on 31 Aug 2019

A = rand(11,6);

out = addedNansRows(A,3,4)

Here addedNansRows:

function out = addedNansRows(A,m,n)

% A - array

% m - namber of rows of A in group

% n - namber of nan in nan-group

s = size(A);

ii = 1:s(1);

iii = (ceil(ii/m)-1)*n;

out = nan([iii(end),0] + s);

out(ii + iii,:) = A;

end

Jos (10584)
on 30 Aug 2019

Despite its simple appearance, this is not a trivial task, for which I created my insertrows function

A = randperm(10).'

B = insertrows(A, NaN, 2:2:numel(A)-1)

INSERTROWS is available for free via the File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/9984-insertrows

Jon
on 30 Aug 2019

Edited: Jon
on 31 Aug 2019

Just as another possibility, this approach is much less general than @Jos but will work for your special case.

I didn't look into how @Jos implements the insertrow function, maybe uses a similar idea, for actually doing the insertion. Anyhow this example should give you some good ideas about how to manipulate the indices.

% define example A matrix, just assign random numbers for example

A = rand(15,4)

% get some dimensions for later

[numRows,numCols] = size(A)

% make indices where rows will go in new matrix

idx = ones(numRows,1);

rownum = 1:numRows;

% put an extra row counter for the rows that are multiples of three

idx(rem(rownum,3)==0) = 2;

% calculate new row numbers by counting up row counters

idx = cumsum(idx);

% make new array B

B = NaN(max(idx),numCols);

% assign rows in B

B(idx,:) = A

You will also find some other approaches if you search MATLAB answers for insert rows for example this one answered by @Guillaume https://www.mathworks.com/matlabcentral/answers/225118-insert-rows-in-a-matrix

I do wonder though why there isn't a MATLAB command to insert rows into specific locations in a matrix. Seems like an issue people would run into with some frequency.

Jon
on 4 Sep 2019

Sorry, I thought you wanted to insert the NaN's before the rows that are multiples of three. It wasn't clear from your initial description as I think you had a typo there. If instead you want to put the NaN's after the rows that are multiples of three, you could use the same approach, just modify the indices as follows:

% put an extra row counter for the row after each multiple of three

idx(rem(rownum-1,3)==0) = 2;

% calculate new row numbers by counting up row counters

% note need to shift count down by 1 as zero is a multiple of 3 and so a

% row would have been inserted before row 1

idx = cumsum(idx) -1;

In anycase, as you can see there are many ways to do the insertion. @Andrei Bobrov's approach which you accepted is quite clean.

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

Start Hunting!