Access the elements from 2D array based on the index

조회 수: 14 (최근 30일)
Pankaja Tanjore
Pankaja Tanjore 2015년 7월 3일
편집: Guillaume 2015년 7월 3일
Hello, I have an array A array with dimension 2x6 A=[10 20 30 40 50 60; 70 80 90 100 110 120];
and another 1D array with dimension 1x2 Indicies_Arr=[3 4];
Now i need to extract the data from Indicies_Arr that is 3 and 4 and extract corresponding element from A. I need to extract A(1,3) ,A(2,3) ,A(1,4) and A(2,4). I have to get the following output A(1,3)=30 A(2,3)=90
A(1,4)=40 A(2,4)=100 Function has to generic for any matrix size.
Please let know the function to get the above mentioned data from the matrix.
Looking forward to hear from you at the earliest.
Thanks Pankaja

채택된 답변

Guillaume
Guillaume 2015년 7월 3일
편집: Guillaume 2015년 7월 3일
Note that your Indicies_Arr is badly named since it is actually a column index. I would rename it to column_indices or similar.
It's also not clear what output you want. Your description is not valid syntax for an output argument. Assuming you just want a matrix with the relevant elements:
A = [10 20 30 40 50 60; 70 80 90 100 110 120];
column_indices = [3 4];
out = A(:, column_indices)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by