Array indexing question:
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a 4x4 matrix with integer numbers as below, followed by the command seeking to replace every row and column by blank.
A=[1 1 1 5; 2 3 4 8; 5 6 7 9; 11 4 6 12];
A(1:4, 1:4)=[]
Matlab is returning an error message for subscripted assignment dimension mismatch, but I do not understand why.
Why is the second command in error?
0 Kommentare
Antworten (2)
Esha Bhargava
am 16 Nov. 2015
The above error message can be recieved when you are attempting to assign more elements to a section of a matrix or vector than that section can hold.
These two error messages occur when you
i) Use a colon operator to specify an entire row or column in the destination matrix. ii) Use linear indexing with an incorrectly sized index array.
Common Causes include: You are attempting to replace a section of one matrix with another and the size of the index vector or vectors used to specify the section of the destination matrix to be replaced does not match that of the source matrix. Alternatively, you are attempting to assign values to individual elements using row and column indices, which allocates a submatrix section of the original matrix.
Verify that the size of the index array or arrays agrees with the size of the array you are attempting to assign to the destination matrix. You should verify that the computations that created the index array and the vector or matrix to be assigned are correct. An unexpected size may indicate that the computations that created the matrix were incorrect.
In the example mentioned in the question, a 4 X 4 matrix is being assigned to a single value. Instead of that, you can create another 4 X 4 matrix of zeros and assign it to A.
0 Kommentare
Walter Roberson
am 16 Nov. 2015
You cannot replace entries in a numeric array by blanks. Only character arrays can contain blanks. If you want a 4 x 4 array of blanks you can use
A = repmat(' ', 4, 4);
The code you are using, A(1:4,1:4) = []; would, if it worked, not replace entries with blanks: it would delete entries.
The syntax to delete entries of an array is not entirely consistent. To delete entries in the two-dimensional array A you can use one of the following syntaxes:
A(:,:) = []; %deletes everything leaving A as 0 x 0
A(:,list of columns) = []; %e.g, A(:,3) = [] or A(:,3:4) = []; deletes the columns named
A(list of rows,:) = []; %e.g, A(3,:) = [] or A(3:4,:) = []; deletes the rows named
A(linear indices) = []; %e.g., A([3 8 11]) = []; deletes the elements named giving a column vector result
In particular, you cannot use
A(list of rows,list of columns) = [];
even when the list of rows or list of columns matches all elements. For example with your 4 x 4 array, A(1:4,2) "should be" the same as A(1:end,2) or A(:,2) but only the syntax with the : by itself is recognized as being a deletion.
Why? I don't know. Probably related to the reasons why you need to code [] or {} or '' literally to do a deletion rather than a variable that stores those values. For example,
Empty = [];
A(:,2) = Empty; %NOT permitted
Sometimes in MATLAB, "It just is" is the reason.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!