Does fillmissing2 produce identical results for a matrix and its transposed version?
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 20 Oct 2025 at 0:00
Answered: MathWorks Support Team
on 20 Oct 2025 at 23:23
I have encountered an issue regarding the behavior of the "fillmissing2" function. Specifically, I expected that filling missing values in a 2D matrix and filling missing values in its transposed version (followed by transposing it back) would produce identical results. However, in practice, the results differ.
Accepted Answer
MathWorks Support Team
on 20 Oct 2025 at 0:00
The discrepancy arises due to the way the "nearest" method in fillmissing2 determines which value to use when multiple neighboring elements are equally close to a missing value. Specifically, the function selects the neighbor with the largest Linear Index.
In MATLAB, linear indices are assigned in column-major order. For the following matrix:
A = 1 2 3
4 NaN 6
7 8 9
the linear indices for each element are as follows:
A(1,1): 1
A(2,1): 2
A(3,1): 3
A(1,2): 4
A(2,2): 5
A(3,2): 6
A(1,3): 7
A(2,3): 8
A(3,3): 9
When filling the NaN at A(2,2), the nearest valid neighbors are 4 (A(2,1), index 2), 2 (A(1,2), index 4), 8 (A(3,2), index 6), and 6 (A(2,3), index 8). Among these, A(2,3) has the largest Linear Index (8), so its value (6) is used to fill the missing entry. The result is:
>> fillmissing2(A, 'nearest')
ans =
1 2 3
4 6 6
7 8 9
However, when the matrix is transposed, the arrangement and thus the linear indices of the elements change. This can alter which neighbor has the largest Linear Index, leading to different results after filling and transposing back. Therefore, fillmissing2 does not guarantee identical results for a matrix and its double-transposed version, regardless of the method used. As a workaround, avoid relying on transposition symmetry when using fillmissing2 for missing value interpolation.
0 Comments
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression 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!