From: Chan Chun K Chan on
Hi! Suppose i have a long array, and a loop will find the index of some elements that meet certain requirement, now how to extract all these elements and let them form a new array? Thanks in advance!
From: Matt Fig on
"Chan Chun K Chan" <asdf(a)yahoo.com> wrote in message <hpflsh$57e$1(a)fred.mathworks.com>...
> Hi! Suppose i have a long array, and a loop will find the index of some elements that meet certain requirement, now how to extract all these elements and let them form a new array? Thanks in advance!

Store the indices and use them with the original. Also, it may be that you could use logical indexing instead of a loop. For example.


% Data
A = magic(5);

% Engine 1
IDX = false(size(A));
for ii = 1:numel(A)
if A(ii)>10 % This is the condition
IDX(ii) = 1;
end
end

B = A(IDX)


Now this could be done with logical indexing as follows:

% Engine 2

C = A(A>10)
From: Chan Chun K Chan on
thanks! that's quite enlightening. if i also want to keep track of the index of the elements chosen, what shall i do?
From: Chan Chun K Chan on
Just found out a find command will locate the index.