Selecting elements of logically pruned vector (concatenating indices)

조회 수: 1 (최근 30일)
Ced
Ced 2015년 1월 12일
편집: Ced 2015년 1월 12일
Hi
Given a vector b and a logical vector v_log, is there a straightforward way to extract particular indices of the partial vector b(v_log) without saving it?
In code:
b = randn(20,1); % a vector
v_log = (randn(20,1)>0); % logical vector to select parts of b
ind = [ 2 ; 3 ]; % the elements I'm interested in
c = b(v_log)(ind); % This does not work, but is there a way to do this?
Thanks!

채택된 답변

Kelly Kearney
Kelly Kearney 2015년 1월 12일
I think you'd need to save the indices.
idx = find(v_log);
c = b(idx(ind));
Depending on the size/sparsity of v_log, you may be able to save some time and storage space by only saving the necessary number of indices:
idx = find(vlog, max(ind), 'first');
c = b(idx(ind));

추가 답변 (1개)

Ced
Ced 2015년 1월 12일
편집: Ced 2015년 1월 12일
@Star Strider: Sure thing, thanks. Just for clarity: b is an arbitrary vector. v_log is a logical vector, hence b(v_log) returns a part of b. My question was how to extract certain elements with indices ind of that partial vector b(v_log) without having to save them.
@Kelly Kearney: Thanks, I'll accept that answer and give that a try. The size of v_log in my case is pretty (very) large and is computed in an iterative loop, which is why I was hoping to do this without saving.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by