Trying to select multiple sections in an array where the sections are separated by constant size

조회 수: 2 (최근 30일)
Let's say that I have an array of size 24, i.e arr=zeros(1,24). Now, let's say that I wan't to set indices [5,6,7,8] and [13,14,15,16] and [21,22,23,24] to 1. So how can I address these array sections without loops? I know that I can write: arr([5:8, 13:16, 21:24]) by hand, but what if the array size is unknown, and I wan't to write it in compact way. Thanks

채택된 답변

Stephen23
Stephen23 2018년 3월 26일
편집: Stephen23 2018년 3월 26일
>> arr = zeros(8,3); % or use reshape
>> arr(5:8,:) = 1;
>> arr = reshape(arr,1,24)
arr =
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
Or
>> arr = repmat([0,0,0,0,1,1,1,1],1,3)
arr =
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by