creating Arrys from another big array

조회 수: 2 (최근 30일)
Rica
Rica 2015년 7월 7일
답변: Guillaume 2015년 7월 7일
Hi, I have an array A of the size 1X1000. and i have a vector of Index: index=[1 25 40 63 77 99 100]
is there any easy way in matlab to create such arrays:
Arr1=(A(index(1)): A(index(2)))
Arr2=(A(index(2)): A(index(3)))
Arr3=(A(index(3)): A(index(4)))
and so on...
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 7일
Post a small example
Rica
Rica 2015년 7월 7일
sorry i mad a mistake:this is the corrected question: Hi, I have an array A of the size 1X100 . and i have a vector of Index: index=[1 25 40 63 77 99 100]
is there any easy way in matlab to create such arrays:
Arr1=(A(index(1)): A(index(2)))
Arr2=(A(index(2)): A(index(3)))
Arr3=(A(index(3)): A(index(4)))
and so on...

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

채택된 답변

Thorsten
Thorsten 2015년 7월 7일
편집: Thorsten 2015년 7월 7일
index=[1 25 40 63 77 99 100]
A = rand(1,100);
for i = 1:numel(index)-1
B{i} = A(index(i):index(i+1));
end
Note that the index(2), index(3), ... index(N-1) elements occur twice in B, such that B has numel(index) -2 elements more than A.

추가 답변 (1개)

Guillaume
Guillaume 2015년 7월 7일
Another option to Thorsten's answer:
indices = [1 25 40 63 77 99 100];
A = rand(1, 100);
B = arrayfun(@(s,e) A(s:e), indices(1:end-1), indices(2:end), 'UniformOutput', false);
Note that it while it is possible to create variable names on the fly as in your example. It's a very bad idea. Using a cell array as in our answers is much better.

카테고리

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