Relating values in one array indexed to another
이전 댓글 표시
I was given some arrays that have been indexed in a funny way. I think the idea was to save space because these are multi-million-point arrays.
Here's the compression and indexing method: I have a compressed array of values a_val and corresponding index record numbers a_ind. I also have a much longer array of indices b_ind. For every record index in b_ind there is a matching a_ind. I want to find the values b_val which will correspond to the values in a_val, but will be the same length as b_ind. Here's what I have:
a_ind = [ 2 3 5 6 7 9 11 12 13 15];
a_val = [10 10 20 30 40 40 40 50 60 60];
b_ind = [2 2 2 3 5 5 6 6 7 9 9 9 11 12 12 13 13 15];
I want:
b_val = [10 10 10 10 20 20 30 30 40 40 40 40 40 50 50 60 60 60];
I can do this with a loop:
b_val = NaN(size(b_ind));
for k = 1:length(b_val)
b_val(k) = a_val(a_ind==b_ind(k));
end
but my arrays contain millions of elements and the loop takes several minutes to complete. I'm sure there is some clever way to use ismember, but I can't think of how. Any ideas?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!