How to add a condition in programming coding ?

조회 수: 1 (최근 30일)
Akash Pal
Akash Pal 2022년 12월 7일
편집: Voss 2022년 12월 7일
for v=1:j
simdone = 1;
row = 1;
while (simdone == 1)
if ((Co1ap1{v}(row,i+1) <= Co1ap1{v}(row+1,i+1)) && (Co1ap1{v}(row,i+2) > Co1ap1{v}(row+1,i+2) ))
row = row + 1;
else
Co1ap1{v}(row+1,:) = []; % delete row if condition does not match #here i want to make changes ,i want to keep the all row values ,before if the condition doesnot match then it was deleting the row but now i want to keep the rows not delete the rows
end
if (row == size((Co1ap1{v}),1))
simdone = 0; % break the while loop if index is out of bound
end
end
disp(Co1ap1{v})
end
i+1 is the column number .
before when i was doing the sorting that time if any rows values does not match my if condition then i was removing that rows from my metrix ,but now i want to keep that rows at the end of my metrix ,like no rows should be delete i want to keep my metrix size same .But the metrix row's will follow the condition and then they will be in a metrix.
AS a example
A= [12 20 ; 14 16; 16 22; 18 10; 13 12 ;20 5]
after running the above matlab code the result
A=[12 20;14 16;18 10;20 5]
but now i want to keep the other solutions also at the end with my result , don't want to delete them from my solution set
Like
A=[12 20;14 16;18 10;20 5; 16 22;13 12]

채택된 답변

Voss
Voss 2022년 12월 7일
편집: Voss 2022년 12월 7일
Put all the deleted rows in another matrix, and append them to the end of Co1ap1{v} after the while loop:
% setting up the example:
Co1ap1 = {[12 20 ; 14 16; 16 22; 18 10; 13 12 ;20 5]};
j = 1;
i = 0;
for v=1:j
simdone = 1;
row = 1;
add_to_end = zeros(0,size(Co1ap1{v},2));
while simdone == 1
if Co1ap1{v}(row,i+1) <= Co1ap1{v}(row+1,i+1) ...
&& Co1ap1{v}(row,i+2) > Co1ap1{v}(row+1,i+2)
row = row+1;
else
add_to_end(end+1,:) = Co1ap1{v}(row+1,:);
Co1ap1{v}(row+1,:) = [];
end
if row == size(Co1ap1{v},1)
simdone = 0;
end
end
Co1ap1{v} = [Co1ap1{v}; add_to_end];
disp(Co1ap1{v})
end
12 20 14 16 18 10 20 5 16 22 13 12

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by