Replacing part of NaN matrix with numerical array

3 views (last 30 days)
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!

Answers (1)

Walter Roberson
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
Andrea
Andrea on 19 Oct 2015
That worked perfectly, thank you so much! I always run into problems with notation for indexing matrices.
Another quick question- if I were going to store A in a struct, would there be special notation because of the NaN at the back end of each row?
Walter Roberson
Walter Roberson on 20 Oct 2015
No, the notation is the same whether an array contains NaN or not.

Sign in to comment.

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!