3 views (last 30 days)

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

Image Analyst
on 13 Dec 2019

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
on 14 Dec 2019

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.

Adam Danz
on 14 Dec 2019

Edited: Adam Danz
on 17 Dec 2019

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.

Opportunities for recent engineering grads.

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/496492-how-to-remove-elements-from-the-vector-while-we-save-the-index-of-those-removed-elements#comment_777724

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/496492-how-to-remove-elements-from-the-vector-while-we-save-the-index-of-those-removed-elements#comment_777724

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/496492-how-to-remove-elements-from-the-vector-while-we-save-the-index-of-those-removed-elements#comment_777764

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/496492-how-to-remove-elements-from-the-vector-while-we-save-the-index-of-those-removed-elements#comment_777764

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/496492-how-to-remove-elements-from-the-vector-while-we-save-the-index-of-those-removed-elements#comment_777931

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/496492-how-to-remove-elements-from-the-vector-while-we-save-the-index-of-those-removed-elements#comment_777931

Sign in to comment.