add new rows to a Matrix

15 次查看(过去 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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by