How can I add NaNs to a cell array in positions that match the positions of NaNs in a separate matrix?
이전 댓글 표시
I have a matrix (A) containing some NaNs. I also have a cell array (B) with no NaNs, containing script in every element.
Where NaNs exist in A, I also want NaNs to appear in B.
Below is my failed attempt to do this. The final line of the script results in an error: "Conversion to cell from double is not possible." Any advice would be much appreciated.
A = rand(2); % matrix
A(1,2) = NaN
B = {'fred' 'wilma'; % cell array
'barney' 'betty'};
nan_locations = find(isnan(A)); % index location of NaNs in A
B(nan_locations) = NaN % in B insert NaNs in same locations as A
채택된 답변
추가 답변 (1개)
Vishal Rane
2012년 12월 5일
Use
B{isnan(A)} = NaN
댓글 수: 5
Vishal Rane
2012년 12월 5일
What you were trying to do was replace an entire cell ( () indexing ) in B by NaN. What you must do is replace the contents ( {} indexing ) of that cell by a NaN.
Mark
2012년 12월 5일
Vishal Rane
2012년 12월 5일
Check if A and B are of identical size.
Mark
2012년 12월 5일
Vishal Rane
2012년 12월 5일
Above method will give error if there are no NaN values in A.
Post your code if possible.
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!