Use an array containing indexes to extract a subset of larger array

조회 수: 79 (최근 30일)
Scorp
Scorp 2022년 7월 15일
댓글: Jeffrey Clark 2022년 7월 15일
largeArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
indexes = [5,9,16]
% use the indexes array to extract 3 elements at each index in the largeArray to yield:
subSetArray = [5,6,7 ; 9,10,11; 16,17,18]
  댓글 수: 1
Jeffrey Clark
Jeffrey Clark 2022년 7월 15일
@Scorp, attack problems like this by looking at what you have and then what you want. I won't give you the answer here:
>>largeArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
largeArray =
Columns 1 through 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Columns 18 through 20
18 19 20
>>indexes = [5,9,16]
indexes =
5 9 16
>>subSetArray = [5,6,7 ; 9,10,11; 16,17,18]
subSetArray =
5 6 7
9 10 11
16 17 18
>>note = indexes' % apostrophy converts arrays (look it up)
note =
5
9
16
>>also = 2+indexes' % compare with subSetArray
also =
7
11
18

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 7월 15일
largeArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
largeArray = 1×20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
indexes = [5,9,16]
indexes = 1×3
5 9 16
% use the indexes array to extract 3 elements at each index in the largeArray to yield:
% subSetArray = [5,6,7 ; 9,10,11; 16,17,18]
subSetArray = largeArray(indexes(:)+(0:2))
subSetArray = 3×3
5 6 7 9 10 11 16 17 18
Make sure you don't go off the end of largeArray:
indexes = [5,9,19]
indexes = 1×3
5 9 19
subSetArray = largeArray(indexes(:)+(0:2))
Index exceeds the number of array elements. Index must not exceed 20.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by