How to add a condition in programming coding ?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!