Combining arrays to form indices
이전 댓글 표시
I'm wondering if there's a method to combine 2 linear arrays for matrix indexing purposes.
For example given the arrays:
A = [1 10 15 25 35]
B = [5 13 18 31 52]
How would one create an indexing array such as:
C = [1:5 10:13 15:18 25:31 35:52]
Hopefully that makes sense, thanks for the help!
채택된 답변
추가 답변 (2개)
Chad Greene
2014년 2월 21일
I like dpb's solution. My slower, clunkier solution would have required a loop:
A = [1 10 15 25 35];
B = [5 13 18 31 52];
m = 1;
for n = 1:length(A)
d = A(n):B(n);
C(m:m+length(d)-1) = d;
m = m+length(d);
end
댓글 수: 1
dpb
2014년 2월 21일
The thing in coming up w/ such solutions is to remember that Matlab operators like : are implemented as functions syntactically so that there's always a functional name form (here colon, of course) to use where need a function handle or the like. It's always easier to think of with "real" functions like mean, say, whereas it's easy to forget about the symbols having alias names because they're not used as often.
Andrei Bobrov
2014년 2월 21일
x(B+1)=-1;
x(A)=1;
out = A(1):B(end)+1;
out = out(cumsum(x)>0);
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!