views:

357

answers:

3

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 between 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?

A: 

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.

yuk
Thanks, but that is not what I want. After turning the image to binary I give up using max operation (it was necessary if I process gray-scale images). I use logical OR operation now. Thanks anyways.
Gulcan
A: 

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 ?

High Performance Mark
I'm trying to write my own dilation method. As a warm-up, I tried to do take a 3x3 part of a matrix and assign the maximum value in that part to the whole values in that part. Thank you, your explanation is really helpful. I'm so new to MATLAB, I was confused at the basics. I could not have thought that max operation would give me a 1x3 matrix. I thought it would return a single value, but again, it won't do any good. I should think over it, thanks again.
Gulcan
+2  A: 

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)));
gnovice
Exactly.By the way, if you think of doing this in 3D in the future, I'd first read the sub-image into a temporary variable, and then do max(tmp(:)). Thus, you won't have to worry about dimensionality.
Jonas

related questions