add new rows to a Matrix

10 ビュー (過去 30 日間)
Zarak kh
Zarak kh 2019 年 8 月 30 日
編集済み: Jon 2019 年 9 月 4 日
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
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 8 月 31 日
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
Andrei Bobrov 2019 年 8 月 31 日
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
  2 件のコメント
Zarak kh
Zarak kh 2019 年 9 月 3 日
編集済み: Zarak kh 2019 年 9 月 3 日
Thanks a lot for your code. It really worked but I need to change m and n to get the results I want.

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

その他の回答 (2 件)

Jos (10584)
Jos (10584) 2019 年 8 月 30 日
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
Jon 2019 年 8 月 30 日
編集済み: Jon 2019 年 8 月 31 日
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.
  2 件のコメント
Jon
Jon 2019 年 9 月 4 日
編集済み: Jon 2019 年 9 月 4 日
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.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by