dimension mismatch
5 views (last 30 days)
Show older comments
I have a set of code ,for some RGB images my codes works but for some the coed not work i get an error "Subscripted assignment dimension mismatch",can u tell how to solve it
2 Comments
Wayne King
on 14 Oct 2011
You need to give a simple example of the sizes of your inputs and what you are doing to produce the error.
Accepted Answer
Image Analyst
on 14 Oct 2011
I already gave him the reason why in his original post: http://www.mathworks.com/matlabcentral/answers/18294-dimension-mismatch
0 Comments
More Answers (2)
the cyclist
on 14 Oct 2011
Breakpoint your code just before the line that causes the error. Then look at the sizes of the expressions on the left and right side of the assignment statement. You will likely find that they are not the same size, which is what causes that error message.
Walter Roberson
on 15 Oct 2011
The first time through, trunCoarseImg is not initialized.
When you use ':' as a subscript in the target of an assignment for an uninitialized array, MATLAB gives more flexibility and treats the ':' as meaning "as much as is needed". Each 2D array is also a 3D array with final dimension 1, so in this initialization case, MATLAB says "Okay, the third dimension only needed a range of 1" and permits the initialization.
There are other leeways that are permitted when assigning to a variable that is not initialized: for example, you can assign any data type.
But when a variable is already initialized, and it only a portion of the variable is marked as the destination, then the rules for assigning in to it are different. For example, the values being assigned must be of a data type that can be converted into the existing data type of the variable. You cannot, for example, assign a cell to an element of a numeric array.
One of the other differences when the destination array exists is that you are only permitted to use the the ':' index destination on a trailing singular dimension if the value being assigned is a scalar (and possibly if the source and destination are both column vectors: I would need to test that specific case.) Otherwise, the number of dimensions of the source value must be exactly the same as ndims of the target.
Arguments could be made about what should happen if ':' is used for a trailing singular dimension and the corresponding implicit dimension of the source is 1, but Mathworks choose to implement this as a "sanity check". Code that really needs this kind of functionality can be made to work with some thought and effort, but the situation represents erroneous coding so much of the time that the errors it prevents (by complaining) outweigh the cost of recoding for the few cases where a degenerate trailing dimension is a desirable possibility.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!