How to add a condition in programming coding ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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]
0 commentaires
Réponse acceptée
Voss
le 7 Déc 2022
Modifié(e) : Voss
le 7 Déc 2022
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
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!