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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by