add new rows to a Matrix
15 次查看(过去 30 天)
显示 更早的评论
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
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
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
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.
更多回答(2 个)
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
0 个评论
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
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 Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!