How to remove elements from the vector while we save the index of those removed elements?
45 次查看(过去 30 天)
显示 更早的评论
Hello, I would like to write a code inside the while loop that it removes the elements of the vector based on the criteria while it keeps the index of those removed elements in each iteration?
b=[2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ];
y = b;
while length(y)>4
idx=find(y(1:3:end));
y=y(1:3:end);
disp(y);
disp(idx);
end
3 个评论
dpb
2019-12-13
Indeed, the crystal ball is drawing a blank here. What is "the criteria" to which you refer? find simply returns all nonzero elements in the input argument; is that the criterion?
The element numbers removed would be those of idx if you were to subsequently remove those after the find operation, via y(idx)=[]; but those indices have any meaning only for the first pass and relative to the original input vector b; once removed the resulting y will be some other length and some of those indices will be out of range and none will refer to the same element in the resulting y
As Adam says, without some rationale, it's an impossible to answer question.
回答(3 个)
Image Analyst
2019-12-13
Is this what you want?
b = [2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1];
y = b;
% Find out which elements have a value of more than 4.
indexesToRemove = find(y>4);
y(indexesToRemove) = []; % Remove those indexes, or equivalently y = b(~indexesToRemove)
dpb
2019-12-14
That's simply the complement of the indices you saved...brute force is
>> ix=1:numel(b); ix(1:3:end)=[];
>> ix
ix =
2 3 5 6 8 9 11 12 14 15 17 18 20
>>
If you tell the decoding algorithm what decimation factor is, it can compute those on its own; doesn't need to be passed.
0 个评论
Adam Danz
2019-12-14
编辑:Adam Danz
2019-12-17
I'll admit to still not fully comprehending the problem though that fault may be on my end. From what I understand you want to recursively remove every n indices within a loop and you want to keep track of which indices are kept and tossed. I think the best way to manage that is by using a matrix of logical vectors where each column identifies the values of the vector b that you keep(1) or toss(0).
Here's a demo.
b=[2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ];
n = 3; % keep every 'n' values
% compute number of columns
% This would be the number of iterations of your while-loop
% given n plus one column (you'll see why in the results section)
nCol = floor(log(numel(b))/log(n)) + 1;
% Initialize logical matrix
idx = false(numel(b),nCol);
% Identify selected data indices
rowIndices = n.^((1:nCol)-1);
for i = 1:nCol
idx(1:rowIndices(i):end,i) = true;
end
Results
For these data, idx is 20 x 3 (below).
Column 1 can be used when you want to select all of the values in b
b(idx(:,1))
Column 2 is every 3rd value in b
b(idx(:,2)) %keep
b(~idx(:,2)) %toss
Column 3 is every 3rd value of the values kept in the previous column, and so on....
1 1 1
1 0 0
1 0 0
1 1 0
1 0 0
1 0 0
1 1 0
1 0 0
1 0 0
1 1 1
1 0 0
1 0 0
1 1 0
1 0 0
1 0 0
1 1 0
1 0 0
1 0 0
1 1 1
1 0 0
Sorry if my interpretation is way off but if it's on target, this is a very easy way to select values in b without breaking apart your data.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!