remove rows from a cell array until condition doesn't met
1 view (last 30 days)
Show older comments
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 Comments
Bob Thompson
on 15 Mar 2018
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.
Answers (1)
Bob Thompson
on 15 Mar 2018
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 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!