Can someone explain this example of deleting elements from a matrix in MATLAB?

本秂侑毒 提交于 2019-11-29 08:48:24

The example you gave shows linear indexing. When you have a multidimensional array and you give it a single scalar or vector, it indexes along each column from top to bottom and left to right. Here's an example of indexing into each dimension:

mat = [1 4 7; ...
       2 5 8; ...
       3 6 9];
submat = mat(1:2, 1:2);

submat will contain the top left corner of the matrix: [1 4; 2 5]. This is because the first 1:2 in the subindex accesses the first dimension (rows) and the second 1:2 accesses the second dimension (columns), extracting a 2-by-2 square. If you don't supply an index for each dimension, separated by commas, but instead just one index, MATLAB will index into the matrix as though it were one big column vector:

submat = mat(3, 3);     % "Normal" indexing: extracts element "9"
submat = mat(9);        % Linear indexing: also extracts element "9"
submat = mat([1 5 6]);  % Extracts elements "1", "5", and "6"

See the MATLAB documentation for more detail.

It's very simple.

It basically starts from the second element in this example and goes upto tenth element (column wise) in steps of 2 and deletes corresponding elements. The remaining elements result in a row vector.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!