Adding rows up to a certain value (cell arrays)
Show older comments
I have a cell (curCell_res) which has two columns (column 1 is name and column 2 is the value). I need 230 from column2. This means I need to take everything from A1 to B5 (220) + 10 from C1.
'A1' 27.5
'A2' 16.5
'A3' 22
'A4' 22
'A5' 22
'B1' 27.5
'B2' 16.5
'B3' 22
'B4' 22
'B5' 22
'C1' 27.5
'C2' 16.5
'C3' 22
'C4' 22
'C5' 22
'D1' 27.5
'D2' 16.5
'D3' 22
'D4' 22
'D5' 22
I need to get to an answer cell that looks like this:
'A1' 27.5
'A2' 16.5
'A3' 22
'A4' 22
'A5' 22
'B1' 27.5
'B2' 16.5
'B3' 22
'B4' 22
'B5' 22
'C1' 10
I started writing the code:
DEMAND = 230;
for i = 1:size(BIG,1) %this is the BIG cell array from which curCell_res comes
curCell_res = BIG{i};
for k=1:size(curCell_res,1)
if k==1
Sum_Power(k) = curCell_res(k,2);
elseif k > 1
Sum_Power(k) = curCell_res(k,2) + Sum_Power(k-1);
if Sum_Power(k) > DEMAND
surplus = Sum_Power(k) - DEMAND; %surplus is not needed
not_surplus = curCell_res(k,2) - surplus; the needed bit we have to take
I have problems continuing, I don't know how to formulate the part where I store the the sequence of numbers and the names. Furthermore, I keep on getting errors such as "conversion from double to cell is not possible"... Can you tell me how I should proceed?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!