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!

 채택된 답변

dpb
dpb 2014년 2월 21일

1 개 추천

One way...
C=cell2mat(arrayfun(@colon,A,B,'uniformoutput',false));

댓글 수: 1

Robert
Robert 2014년 2월 21일
Thank you! That works perfectly for my application.

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

추가 답변 (2개)

Chad Greene
Chad Greene 2014년 2월 21일

0 개 추천

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
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
Andrei Bobrov 2014년 2월 21일

0 개 추천

x(B+1)=-1;
x(A)=1;
out = A(1):B(end)+1;
out = out(cumsum(x)>0);

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2014년 2월 21일

답변:

2014년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by