I tried to update some part of a matrix, I got the following error message:
??? Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts
My code tries to update some values of a matrix that represent a binary image. My code is as follows:
outImage(3:5,2:4,1) = max(imBinary(3:5,2:4,1));
When I delete last parameter (1), this time I get the same error. I guess there is a mismatch bet开发者_如何学运维ween dimensions but I could not get it. outImage is a new object that is created at that time (I tried to create it before, but nothing changed). What may be wrong?
You mention in one of your comments on another answer that you are trying to create your own dilation algorithm, and therefore want to take the maximum value in a 3-by-3-by-1 submatrix and replace the values in that submatrix with the maximum value. The function MAX will by default operate along the columns of your submatrix, which will give you a 1-by-3 matrix (i.e. the maximum values of the columns of your 3-by-3-by-1 matrix). The error results because MATLAB can't assign a 1-by-3 matrix to a 3-by-3-by-1 matrix.
One solution is to call MAX again on your 1-by-3 matrix to get a scalar value, which you can then assign to each element of your 3-by-3-by-1 submatrix without error:
outImage(3:5,2:4,1) = max(max(imBinary(3:5,2:4,1)));
On the rhs of your equation you take the max of a 3x3x1 sub-matrix, which returns a 1x3 vector. You then try to assign this to a 3x3x1 sub-matrix. A singleton subscript is one with the value 1. So the rhs has 1 non-singleton subscript, and the lhs has 2. Matlab can't figure out how to expand a 1x3 matrix to fill a 3x3x1 space.
I'm not entirely sure what you want to do, so I won't guess a solution. Do you want to make 3 copies of the rhs and put one into each row of the sub-matrix on the lhs ? Or are you trying to construct a 3x3x1 matrix on the rhs ?
Do you want to fill all indexed elements in outImage by maximum value for each column of rhs expression? You can expand the row you get on rhs with REPMAT:
outImage(3:5,2:4,1) = repmat(max(imBinary(3:5,2:4,1)),3,1)
outImage(3:5,2:4)
works as well.
I got the same error before, and what I have done was defining the left hand matrix before. I don't know if you have the same case but you can try the following:
outImage=Zeros(M,N,K);
M, N, and K are the dimensions that you have. Then just type:
outImage(3:5,2:4,1) = max(max(imBinary(3:5,2:4,1)));
精彩评论