Replacing part of NaN matrix with numerical array
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2015-10-19
编辑:Walter Roberson
2015-10-20
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!