Why does it become four number in a line?

1 Ansicht (letzte 30 Tage)
zhenyu zeng
zhenyu zeng am 1 Mär. 2019
Kommentiert: Steven Lord am 1 Mär. 2019
>>A(0 0 0;0 2 2)
>>A(1:2)=[]
A=
0 2 0 2
Why A becomes 0 2 0 2 not becomes (0 2;0 2)?

Akzeptierte Antwort

Stephan
Stephan am 1 Mär. 2019
Hi,
you do linear indexing which lets Matlab reshape A. To avoid this use:
A(:,1)=[]
Best regards
Stephan
  3 Kommentare
Stephan
Stephan am 1 Mär. 2019
There are 2 ways of indexing in Matlab. Depending on what you want to do the one maybe better or the other. Knowing both and how they behave lets you make the right decision. I think having 2 possibilities is the advantage.
Steven Lord
Steven Lord am 1 Mär. 2019
If you delete elements using linear indices, if MATLAB didn't reshape the resulting elements into a vector you could end up with an array with different numbers of elements in each row or column. That's not allowed. Arrays in MATLAB must be "rectangular".
thisWillNotWork = [1 2; 3]
In your particular case the elements remaining after the deletion would still be arranged in a rectangle. But in general they wouldn't. Ideally you shouldn't need to check after performing that deletion operation what shape the result is.
In fact, even if you don't delete any elements at all we reshape the result. You tried to delete elements, so we put your data in the shape of a vector.
A = magic(4);
A([]) = [];
size(A) % [1 16]

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

TADA
TADA am 1 Mär. 2019
you accessed a 2d matrix with linear indexing,
If you want to retain the dimentions of your matrix use subscripts instead:
A(:,1) = []
A =
0 0
2 2

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by