dimension mismatch
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Wayne King
2011년 10월 14일
You need to give a simple example of the sizes of your inputs and what you are doing to produce the error.
채택된 답변
Image Analyst
2011년 10월 14일
I already gave him the reason why in his original post: http://www.mathworks.com/matlabcentral/answers/18294-dimension-mismatch
댓글 수: 0
추가 답변 (2개)
the cyclist
2011년 10월 14일
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
2011년 10월 15일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!