MATLAB Answers

# creating additonal rows of NaN in specific positions

49 views (last 30 days)
antonet on 7 Aug 2012
Commented: William Garrett on 22 Feb 2020
Dear all,
I have a large matrix (double array of dimension 80000 by 21) and I want to create NaN rows in specific positions.
I have set up a rule [find(gg>300)] according to which I know the positions at which I want to create an additional row that will contain NaNs
For instance, let’s start with a simple example
A=[
2.4332 4.1477;
2.6073 4.5900;
2.2310 3.7442;
2.4555 4.1178;
2.5096 4.1946;
2.7517 4.7802;
2.8372 4.9423;
2.9577 5.1563;
3.2365 5.6061;
3.0658 5.3787;
2.9244 5.0497;
2.6104 4.4623;
2.5419 4.4164;
2.4947 4.3577;
2.5633 4.7050
]
Suppose that the criterion “find” tells me that I have to create additional rows of NaNS in positions 2,4,5,10 Then I want to have
A=[
2.4332 4.1477;
2.6073 4.5900;
NaN NaN
2.2310 3.7442;
2.4555 4.1178;
NaN NaN
2.5096 4.1946;
NaN NaN
2.7517 4.7802;
2.8372 4.9423;
2.9577 5.1563;
3.2365 5.6061;
3.0658 5.3787;
NaN NaN
2.9244 5.0497;
2.6104 4.4623;
2.5419 4.4164;
2.4947 4.3577;
2.5633 4.7050
]
thanks

#### 0 Comments

Sign in to comment.

### Accepted Answer

Oleg Komarov on 7 Aug 2012
Very similar to your previous question. You have to build the idx accordingly. Nte that the idx stores the row position where you will allocate A in Anew.
pos = [2,4,5,10];
[r,c] = size(A);
add = numel(pos); % How much longer Anew is
Anew = NaN(r + add,c); % Preallocate
idx = setdiff(1:r+add,pos); % all positions of Anew except pos
Anew(idx,:) = A;

#### 2 Comments

antonet on 7 Aug 2012
thanks Oleg!
William Garrett on 22 Feb 2020
Hi, I have tried this approach but after the final step I recieve the following error:
'The following error occurred converting from datetime to double: Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions'
Are you able to explain to me what I need to do?

Sign in to comment.