필터 지우기
필터 지우기

Replacing part of NaN matrix with numerical array

조회 수: 2 (최근 30일)
Andrea
Andrea 2015년 10월 19일
댓글: Walter Roberson 2015년 10월 20일
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!

답변 (1개)

Walter Roberson
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
Andrea
Andrea 2015년 10월 19일
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 2015년 10월 20일
No, the notation is the same whether an array contains NaN or not.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by