add new rows to a Matrix

12 ビュー (過去 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 件のコメント
Jon
Jon 2019 年 9 月 3 日
Quite ingenious way to calculate the row indices in the new larger matrix where the original rows of A get written. I had to step through this with the debugger a few times to understand how it worked. Also generalizes the original OP's request to insert a single row of NaN to allow inserting an arbitrary number of NaN rows.
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 件のコメント
Zarak kh
Zarak kh 2019 年 9 月 3 日
Dear Jon
Thanks for your help,But I think something is not working well with the code you privided.
I think the probem is in this section.Since the rownumber are from Matrix A, the NaN will be after each number which is multiples of three in matrix A.
% put an extra row counter for the rows that are multiples of three
idx(rem(rownum,3)==0) = 2;
So the result is :
B =
0.8843 0.1897 0.9900 0.6609
0.5880 0.4950 0.5277 0.7298
NaN NaN NaN NaN
0.1548 0.1476 0.4795 0.8908
0.1999 0.0550 0.8013 0.9823
0.4070 0.8507 0.2278 0.7690
NaN NaN NaN NaN
0.7487 0.5606 0.4981 0.5814
0.8256 0.9296 0.9009 0.9283
0.7900 0.6967 0.5747 0.5801
NaN NaN NaN NaN
0.3185 0.5828 0.8452 0.0170
0.5341 0.8154 0.7386 0.1209
0.0900 0.8790 0.5860 0.8627
NaN NaN NaN NaN
0.1117 0.9889 0.2467 0.4843
0.1363 0.0005 0.6664 0.8449
0.6787 0.8654 0.0835 0.2094
NaN NaN NaN NaN
0.4952 0.6126 0.6260 0.5523
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.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by