How to delete multiple elements from array in step by step process and how to store remaining elements in array

조회 수: 4 (최근 30일)
I have an array A=[1 0.2 7 0.5 3 8]
I want to delete lowest number from this array
A(A==0.2)=[]
A=[1 7 0.5 3 8]
now again I want to delet lowest number from this
A(A==0.5)=[]
A=[1 7 3 8]
Now, How do I store different lengths array for future reference.
array={[1 0.2 7 0.5 3 8];
[1 7 0.5 3 8];
[1 7 3 8];
[7 3 8];
[7 8];
[8]}
is it possible with while loop ???

채택된 답변

DGM
DGM 2021년 5월 2일
For storing dissimilar size/type objects, you can use a cell array:
A = [1 0.2 7 0.5 3 8];
B = cell(numel(A),1);
B{1} = A;
for v = 2:numel(A)
thisvec = B{v-1};
[~,idx] = min(thisvec);
thisvec(idx) = [];
B{v} = thisvec;
end
B{:}
gives
ans =
1.0000 0.2000 7.0000 0.5000 3.0000 8.0000
ans =
1.0000 7.0000 0.5000 3.0000 8.0000
ans =
1 7 3 8
ans =
7 3 8
ans =
7 8
ans =
8
  댓글 수: 3
DGM
DGM 2021년 5월 2일
편집: DGM 2021년 5월 2일
You could use a while loop, but there's no good reason to use a while loop if the number of iterations is known. It's a lot harder to accidentally make a for-loop become infinite.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2021년 5월 2일
A = [1 0.2 7 0.5 3 8];
nA = numel(A);
array = cell(nA, 1);
for k = 1:nA
array{k} = maxk(A, nA - k + 1);
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by