"Subscripted assignment dimension mismatch" when using "find" with a tolerance?
1 次查看(过去 30 天)
显示 更早的评论
My code gets pretty in depth so I am not sure if posting it all would be helpful at all. Essentially I am using find with a tolerance to obtain the common numbers between two matrices. Matrix N_new is a 884x884x3 layered matrix and FGIF is a 801x2 matrix. I keep getting the error for the line in the for loop saying "Subscripted assignment dimension mismatch." Any speculation as to why I would be recieving this error? I have played with the tolerance and it does not make a difference. I am pretty sure that all of the values in FGIF are a part of matrix N within the tolerance specified. I have provided a little code to get an idea of what I am going for.
[FGIF_r, FGIF_c] = size(FGIF);
tol = 0.001;
for i = 1:FGIF_r
[row(i), col(i)] = find(abs(N_new(:,:,2)-FGIF(i,1))<=tol & abs(N_new(:,:,3)-FGIF(i,2))<=tol);
end
0 个评论
回答(2 个)
Steven Lord
2017-8-21
As written, you're assuming that exactly 1 (no more, no fewer) of the elements of the expression inside the find function call are nonzero. From the fact that you're receiving an error it seems that assumption is violated. Set an error breakpoint and run your code. When MATLAB stops at the error breakpoint, run the find call with zero output arguments and see how many elements the ans output contains.
If you know that at least 1 element of the expression inside the find function call will be nonzero and you want to find the first such value, call find with two inputs where the second input (denoted by n in the documentation) is 1.
4 个评论
Walter Roberson
2017-8-23
"Is there a way to tell matlab that if it runs into an empty set during find to just remove the entire row?"
No.
You can assign the output of find() to temporary variables, and test the variables before doing the assignment.
However, for efficiency, instead of doing the deletion immediately, it is better to keep track of which entries to save (or delete), and then after the loop, do the appropriate processing. This also reduces the chances of accidentally missing processing an entry or accidentally running off the end of the matrix by failing to take into account that all the entries after the one being deleted "fall down to fill the hole", Tetris-like.
Walter Roberson
2017-8-21
For testing purposes, try
[FGIF_r, FGIF_c] = size(FGIF);
tol = 0.001;
for i = 1:FGIF_r
[row{i}, col{i}] = find(abs(N_new(:,:,2)-FGIF(i,1))<=tol & abs(N_new(:,:,3)-FGIF(i,2))<=tol);
end
I suspect that you will find that some entries are either empty or else have more than one result. In particular I suspect you might have entries that are duplicates to within tolerance.
I wonder if you should perhaps be using ismembertol() with the 'byrows' option.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!