MATLAB: How to extract value from some row or column in a matrix

how to extract value from some row or column in a matrix

Dear members
I have two matrix
A is a matrix that has 100 row=[1 2 3 4 5 6 7 8 9 10 ……100]
B is a matrix that wants to get the value from row 1 to row 10, and from row 25 to row 35
How to get matrix B
Thank you

Best Answer

By indexing like shown below:
% if A is a matrix
A=rand(100); % fake data
B=A([1:10 25:35],:) % extracts specified rows
B=A(:,[1:10 25:35]) % extracts specified columns
% if A is a vector then
A=1:100; % fake data (row vector) , (1:100).' - column vector
B=A([1:10 25:35]) % extracts specified elements (1D-a vector)
Note: The square brackets [] is used to denote discontinuities thereby extracting the spcified elements, rows or columns.