MATLAB: copy a specific part of an array

I am trying to copy multiple elements from a matrix, but not a whole row, not a single element.

For example, in the following matrix:

a = 1 2 3 4 5 6 7 8 9 0 

How to copy only the following data?

 b = 1 3 5 

i.e. rows 1: 3 only in column 1 ... I know that you can delete the entire column as follows:

 b = a(:,1) 

... and I appreciate that I could just do it and then flush the last two lines, but I would like to use a more ordered code, as I am running a very resource-intensive solution.

+4
source share
2 answers

Elements in a matrix in MATLAB are stored in column order. This means that you can even use one index and say:

 b = a(1:3); 

Since the first 3 elements are ARE 1,3,5. Similarly, a (6) is 2, a (7) is 4, and so on. Take a look at the sub2ind method to understand more:

http://www.mathworks.com/help/techdoc/ref/sub2ind.html

+4
source

You do not β€œdelete” the second column; you are referring to another column.

You should read some of the Matlab docs, they provide some help with the syntax for accessing parts of matrices:

http://www.mathworks.com/help/techdoc/learn_matlab/f2-12841.html#f2-428

+1
source

Source: https://habr.com/ru/post/1395707/


All Articles