Replacing part of NaN matrix with numerical array
2 views (last 30 days)
Show older comments
I have an 8x150 NaN matrix created to avoid a time lag in running my code. During a for loop, I create an array of values that I'd like to insert into a row of the matrix that corresponds to the index of the loop. These must be stored individually in the correct row in matrix A.
A = nan(8,150);
for k = 2:7;
A (k,:) = [start:bins:end]; %start, bins, and end are created earlier in the loop without any problems
end
I keep getting errors about subscript assignment and dimension mixmatch. Some of the rows will have different lengths due to the nature of the data. Is it not possible to only replace a small stretch with values and leave the rest as NaN?
Thank you in advance for your help!
0 Comments
Answers (1)
Walter Roberson
on 19 Oct 2015
Edited: Walter Roberson
on 20 Oct 2015
Where in the row do you want to insert it?
When you use ':' by itself as a subscript and the array already exists, then the size of what you store has to exactly match the size of what is there already. If you only want to store to part of a row then you need to indicate which part of the row to store into. For example,
newvals = start:bins:endvalue;
A(k,1:length(newvals)) = newvals;
Note: do not name a variable 'end': 'end' has special meaning in arrays!
Also, if you are concerned about performance then please note that working with NaN requires alternate pathways be taken in the processor, as the processor has to keep checking if the NaN is a "Signalling NaN" that should be triggering an exception. You can achieve higher performance by using a different marker value, perhaps one of the infinities.
2 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!