remove rows from a cell array until condition doesn't met
1 次查看(过去 30 天)
显示 更早的评论
U=find(AGe<CONS_T3);
lets take U=11 then remove rows from CNSSS{11,1} and update CONS_T3=(CONS_T3-removed row) each time until the CONS_T3<=AGe. simillary do for all U values. I tried this but can not get results...(CONS_T3 is sum of CNSSS of all rows)
for j=1:length(U)
CONS_T3=CONS_T3-CNSSS{U(j),1}(1,:);
CNSSS{U(j),1}(1,:)=[];
if CONS_T3(U(j))>AGe(U(j))
CONS_T3=CONS_T3-CNSSS{U(j),1}(1,:);
CNSSS{U(j),1}(1,:)=[];
end
end
2 个评论
Bob Thompson
2018-3-15
What type of variables are AGe, CONS_T3, CNSSS, and U?
It looks like the if statement isn't really doing a whole lot, since you just have the previous commands repeated again.
回答(1 个)
Bob Thompson
2018-3-15
If you're trying to just get rid of all U rows then you will want to use something more like this:
U=find(AGe<CONS_T3);
for j=1:length(U)
if U(j) == 1; % Check to see if U(j) == 1 because matrixing from 1:0 doesn't make sense
CONS_T3 = CONS_T3(2:end,:); % Remove first row if first row is bad
else
CONS_T3=vertcat(CONS_T3(1:U(j)-1,:),CONS_T3(U(j)+1:end,:)) % There's not real reason to bring CNSSS into this, because all the values of CNSSS are already part of CONS_T3
CNSSS{U(j),1}(1,:)=[]; % You can leave this if you would like, but it's not really necessary
end % U(j) check if
if sum(CONS_T3)>sum(AGe) % If statement to check of AGe is greater than CONS_T3 yet.
break % Break the loop to stop removing elements. If you just want to remove all the elements anyway, then just get rid of this if statement
end % summation check if
end U(j) loop
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!