Replacing part of NaN matrix with numerical array
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Walter Roberson
el 19 de Oct. de 2015
Editada: Walter Roberson
el 20 de Oct. de 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 comentarios
Walter Roberson
el 20 de Oct. de 2015
No, the notation is the same whether an array contains NaN or not.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!