Use an array containing indexes to extract a subset of larger array
79 次查看(过去 30 天)
显示 更早的评论
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
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
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]
subSetArray = largeArray(indexes(:)+(0:2))
Make sure you don't go off the end of largeArray:
indexes = [5,9,19]
subSetArray = largeArray(indexes(:)+(0:2))
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!