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
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.
mukesh kumar
mukesh kumar 2018-3-15
AGe(size 1*24) aggregator energy and CONS_T3(1*24 size) total energy consumption in each hours that is the sum of all loads CNSSS in each hours,if AGe<CONS_T3 then remove load one by one and update CONS_T3 until AGe>=CONS_T3. thanks for support

请先登录,再进行评论。

回答(1 个)

Bob Thompson
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 个评论
mukesh kumar
mukesh kumar 2018-3-16
编辑:mukesh kumar 2018-3-16
it shows error of Index exceeds matrix dimensions. all U will be removed when one by one remove rows from CNSSS with updating each time CONS_T3 =cONS_T3-that row. thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by