Translating individual pieces of code to loop statement












0















I am working on matlab with a file composed of units requested in different days (orders of products). The file that can be found in the next link:File



It has 4 columns. The second and fourth show the day and number of units respectively. This file has 3000 rows and I would like to do this task for all the entire rows in the file. The main task of the code I will show consist of filling column 6 with 1 every time the value calculated in column 5 is below 300. The column 5 shows the reduction of volume of units that initially is 1000. I will show with the next code:



clear
%Data
File='File.csv';
dataf=csvread(File, 1);
unbl=0;
dataf=[dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)];
%Loop by pieces
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
end
end


I1



In line 29, there is a value below 300 for column 5, therefore 1 in column 6. The number of day in column 2 is 3. Then I have to add 4 to this and find the row when this happens first. In this case the first 7. This code makes that and saves the value of column 5 in the previous row in unlb:



index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);


In this row, I will have new stock then I have to add to the actual value of column 5 the quantity 1200:



dataf(index2,5) = 1200+dataf(index2,5);


I got this:



I2



Therefore, from this point I have to repeat the same task. I use this long code:



for j=(index2+1):size(dataf,1)
dataf(j,5)=dataf(j-1,5)-dataf(j,4);
if dataf(j,5)<300 && dataf(j-1,5)>=300
dataf(j,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for k=(index2+1):size(dataf,1)
dataf(k,5)=dataf(k-1,5)-dataf(k,4);
if dataf(k,5)<300 && dataf(k-1,5)>=300
dataf(k,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for l=(index2+1):size(dataf,1)
dataf(l,5)=dataf(l-1,5)-dataf(l,4);
if dataf(l,5)<300 && dataf(l-1,5)>=300
dataf(l,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);


This code does the task for some remaining rows but if I have to complete the entire dataset it will be too large. I tried a one loop solution but unfortunately it does not work:



%One loop (not working)
index=0;
index2=0;
unbl=0;
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
end
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);
i=index2+1;
end


I do not know how to update the index of the loop to move across all rows and get the cumulative value saved in unbl. Please could you help to fix this pieces of code into one loop statement to move around all rows and reproduce the task previously mentioned. Many Thanks.










share|improve this question























  • not an answer: [dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)] = [dataf zeros(size(dataf,1),2)]

    – EBH
    Nov 24 '18 at 20:39













  • Not sure what you try to do, but this is certainly not valid: i=index2+1 - you should not (and as far as I know can not) change the loop iterator inside a for loop.

    – EBH
    Nov 24 '18 at 20:58
















0















I am working on matlab with a file composed of units requested in different days (orders of products). The file that can be found in the next link:File



It has 4 columns. The second and fourth show the day and number of units respectively. This file has 3000 rows and I would like to do this task for all the entire rows in the file. The main task of the code I will show consist of filling column 6 with 1 every time the value calculated in column 5 is below 300. The column 5 shows the reduction of volume of units that initially is 1000. I will show with the next code:



clear
%Data
File='File.csv';
dataf=csvread(File, 1);
unbl=0;
dataf=[dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)];
%Loop by pieces
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
end
end


I1



In line 29, there is a value below 300 for column 5, therefore 1 in column 6. The number of day in column 2 is 3. Then I have to add 4 to this and find the row when this happens first. In this case the first 7. This code makes that and saves the value of column 5 in the previous row in unlb:



index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);


In this row, I will have new stock then I have to add to the actual value of column 5 the quantity 1200:



dataf(index2,5) = 1200+dataf(index2,5);


I got this:



I2



Therefore, from this point I have to repeat the same task. I use this long code:



for j=(index2+1):size(dataf,1)
dataf(j,5)=dataf(j-1,5)-dataf(j,4);
if dataf(j,5)<300 && dataf(j-1,5)>=300
dataf(j,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for k=(index2+1):size(dataf,1)
dataf(k,5)=dataf(k-1,5)-dataf(k,4);
if dataf(k,5)<300 && dataf(k-1,5)>=300
dataf(k,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for l=(index2+1):size(dataf,1)
dataf(l,5)=dataf(l-1,5)-dataf(l,4);
if dataf(l,5)<300 && dataf(l-1,5)>=300
dataf(l,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);


This code does the task for some remaining rows but if I have to complete the entire dataset it will be too large. I tried a one loop solution but unfortunately it does not work:



%One loop (not working)
index=0;
index2=0;
unbl=0;
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
end
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);
i=index2+1;
end


I do not know how to update the index of the loop to move across all rows and get the cumulative value saved in unbl. Please could you help to fix this pieces of code into one loop statement to move around all rows and reproduce the task previously mentioned. Many Thanks.










share|improve this question























  • not an answer: [dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)] = [dataf zeros(size(dataf,1),2)]

    – EBH
    Nov 24 '18 at 20:39













  • Not sure what you try to do, but this is certainly not valid: i=index2+1 - you should not (and as far as I know can not) change the loop iterator inside a for loop.

    – EBH
    Nov 24 '18 at 20:58














0












0








0








I am working on matlab with a file composed of units requested in different days (orders of products). The file that can be found in the next link:File



It has 4 columns. The second and fourth show the day and number of units respectively. This file has 3000 rows and I would like to do this task for all the entire rows in the file. The main task of the code I will show consist of filling column 6 with 1 every time the value calculated in column 5 is below 300. The column 5 shows the reduction of volume of units that initially is 1000. I will show with the next code:



clear
%Data
File='File.csv';
dataf=csvread(File, 1);
unbl=0;
dataf=[dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)];
%Loop by pieces
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
end
end


I1



In line 29, there is a value below 300 for column 5, therefore 1 in column 6. The number of day in column 2 is 3. Then I have to add 4 to this and find the row when this happens first. In this case the first 7. This code makes that and saves the value of column 5 in the previous row in unlb:



index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);


In this row, I will have new stock then I have to add to the actual value of column 5 the quantity 1200:



dataf(index2,5) = 1200+dataf(index2,5);


I got this:



I2



Therefore, from this point I have to repeat the same task. I use this long code:



for j=(index2+1):size(dataf,1)
dataf(j,5)=dataf(j-1,5)-dataf(j,4);
if dataf(j,5)<300 && dataf(j-1,5)>=300
dataf(j,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for k=(index2+1):size(dataf,1)
dataf(k,5)=dataf(k-1,5)-dataf(k,4);
if dataf(k,5)<300 && dataf(k-1,5)>=300
dataf(k,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for l=(index2+1):size(dataf,1)
dataf(l,5)=dataf(l-1,5)-dataf(l,4);
if dataf(l,5)<300 && dataf(l-1,5)>=300
dataf(l,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);


This code does the task for some remaining rows but if I have to complete the entire dataset it will be too large. I tried a one loop solution but unfortunately it does not work:



%One loop (not working)
index=0;
index2=0;
unbl=0;
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
end
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);
i=index2+1;
end


I do not know how to update the index of the loop to move across all rows and get the cumulative value saved in unbl. Please could you help to fix this pieces of code into one loop statement to move around all rows and reproduce the task previously mentioned. Many Thanks.










share|improve this question














I am working on matlab with a file composed of units requested in different days (orders of products). The file that can be found in the next link:File



It has 4 columns. The second and fourth show the day and number of units respectively. This file has 3000 rows and I would like to do this task for all the entire rows in the file. The main task of the code I will show consist of filling column 6 with 1 every time the value calculated in column 5 is below 300. The column 5 shows the reduction of volume of units that initially is 1000. I will show with the next code:



clear
%Data
File='File.csv';
dataf=csvread(File, 1);
unbl=0;
dataf=[dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)];
%Loop by pieces
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
end
end


I1



In line 29, there is a value below 300 for column 5, therefore 1 in column 6. The number of day in column 2 is 3. Then I have to add 4 to this and find the row when this happens first. In this case the first 7. This code makes that and saves the value of column 5 in the previous row in unlb:



index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);


In this row, I will have new stock then I have to add to the actual value of column 5 the quantity 1200:



dataf(index2,5) = 1200+dataf(index2,5);


I got this:



I2



Therefore, from this point I have to repeat the same task. I use this long code:



for j=(index2+1):size(dataf,1)
dataf(j,5)=dataf(j-1,5)-dataf(j,4);
if dataf(j,5)<300 && dataf(j-1,5)>=300
dataf(j,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for k=(index2+1):size(dataf,1)
dataf(k,5)=dataf(k-1,5)-dataf(k,4);
if dataf(k,5)<300 && dataf(k-1,5)>=300
dataf(k,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);

for l=(index2+1):size(dataf,1)
dataf(l,5)=dataf(l-1,5)-dataf(l,4);
if dataf(l,5)<300 && dataf(l-1,5)>=300
dataf(l,6)=1;
end
end

index =find(dataf(:,6)==1,1,'last');
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);


This code does the task for some remaining rows but if I have to complete the entire dataset it will be too large. I tried a one loop solution but unfortunately it does not work:



%One loop (not working)
index=0;
index2=0;
unbl=0;
for i=1:size(dataf,1)
if i==1
dataf(i,5)=1000-dataf(i,4);
else
dataf(i,5)=dataf(i-1,5)-dataf(i,4);
end
if dataf(i,5)<300 && dataf(i-1,5)>=300
dataf(i,6)=1;
index =find(dataf(:,6)==1);
index2 = find(dataf(:,2)==dataf(index,2)+4,1);
end
unbl = unbl+dataf(index2-1,5);
dataf(index2,5) = 1200+dataf(index2,5);
i=index2+1;
end


I do not know how to update the index of the loop to move across all rows and get the cumulative value saved in unbl. Please could you help to fix this pieces of code into one loop statement to move around all rows and reproduce the task previously mentioned. Many Thanks.







matlab






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 20:35









DuckDuck

1,14762242




1,14762242













  • not an answer: [dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)] = [dataf zeros(size(dataf,1),2)]

    – EBH
    Nov 24 '18 at 20:39













  • Not sure what you try to do, but this is certainly not valid: i=index2+1 - you should not (and as far as I know can not) change the loop iterator inside a for loop.

    – EBH
    Nov 24 '18 at 20:58



















  • not an answer: [dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)] = [dataf zeros(size(dataf,1),2)]

    – EBH
    Nov 24 '18 at 20:39













  • Not sure what you try to do, but this is certainly not valid: i=index2+1 - you should not (and as far as I know can not) change the loop iterator inside a for loop.

    – EBH
    Nov 24 '18 at 20:58

















not an answer: [dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)] = [dataf zeros(size(dataf,1),2)]

– EBH
Nov 24 '18 at 20:39







not an answer: [dataf zeros(size(dataf,1),1) zeros(size(dataf,1),1)] = [dataf zeros(size(dataf,1),2)]

– EBH
Nov 24 '18 at 20:39















Not sure what you try to do, but this is certainly not valid: i=index2+1 - you should not (and as far as I know can not) change the loop iterator inside a for loop.

– EBH
Nov 24 '18 at 20:58





Not sure what you try to do, but this is certainly not valid: i=index2+1 - you should not (and as far as I know can not) change the loop iterator inside a for loop.

– EBH
Nov 24 '18 at 20:58












1 Answer
1






active

oldest

votes


















0














Here is a piece of code that I believe do what you need (explanations in the comments inside):



stock = 1000;
new_stock = 1200;
unbl = 0;
% culculate the comulative substraction from the initial stock:
temp = cumsum([stock; -dataf(:,4)]);
dataf(:,5) = temp(2:end); % no need for the first element (1000)
% find when to set a new order:
low_stock = find(dataf(:,5)<300,1);
% start the loop
last_low = 0;
while low_stock~=last_low % if you found a new date with low stock
last_low = low_stock; % use the new date
dataf(last_low,6) = 1; % place 1 in the date with low stock
% find the first line 4 days ahead:
refill = last_low+find(dataf(last_low:end,2)==dataf(last_low,2)+4,1)-1;
% add the amount on column 5 in the row above ind_b to 'unbl'
unbl = unbl+dataf(refill-1,5);
% add new stock to all rows below after stock arival
dataf(refill:end,5) = dataf(refill:end,5)+new_stock;
% find the next time to set a new order:
low_stock = refill+find(dataf(refill+1:end,5)<300,1);
end





share|improve this answer


























  • Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

    – Duck
    Nov 24 '18 at 22:17











  • @Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

    – EBH
    Nov 24 '18 at 22:35











  • actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

    – Duck
    Nov 24 '18 at 22:42













  • 1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

    – EBH
    Nov 24 '18 at 22:49











  • yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

    – Duck
    Nov 24 '18 at 22:57













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462148%2ftranslating-individual-pieces-of-code-to-loop-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here is a piece of code that I believe do what you need (explanations in the comments inside):



stock = 1000;
new_stock = 1200;
unbl = 0;
% culculate the comulative substraction from the initial stock:
temp = cumsum([stock; -dataf(:,4)]);
dataf(:,5) = temp(2:end); % no need for the first element (1000)
% find when to set a new order:
low_stock = find(dataf(:,5)<300,1);
% start the loop
last_low = 0;
while low_stock~=last_low % if you found a new date with low stock
last_low = low_stock; % use the new date
dataf(last_low,6) = 1; % place 1 in the date with low stock
% find the first line 4 days ahead:
refill = last_low+find(dataf(last_low:end,2)==dataf(last_low,2)+4,1)-1;
% add the amount on column 5 in the row above ind_b to 'unbl'
unbl = unbl+dataf(refill-1,5);
% add new stock to all rows below after stock arival
dataf(refill:end,5) = dataf(refill:end,5)+new_stock;
% find the next time to set a new order:
low_stock = refill+find(dataf(refill+1:end,5)<300,1);
end





share|improve this answer


























  • Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

    – Duck
    Nov 24 '18 at 22:17











  • @Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

    – EBH
    Nov 24 '18 at 22:35











  • actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

    – Duck
    Nov 24 '18 at 22:42













  • 1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

    – EBH
    Nov 24 '18 at 22:49











  • yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

    – Duck
    Nov 24 '18 at 22:57


















0














Here is a piece of code that I believe do what you need (explanations in the comments inside):



stock = 1000;
new_stock = 1200;
unbl = 0;
% culculate the comulative substraction from the initial stock:
temp = cumsum([stock; -dataf(:,4)]);
dataf(:,5) = temp(2:end); % no need for the first element (1000)
% find when to set a new order:
low_stock = find(dataf(:,5)<300,1);
% start the loop
last_low = 0;
while low_stock~=last_low % if you found a new date with low stock
last_low = low_stock; % use the new date
dataf(last_low,6) = 1; % place 1 in the date with low stock
% find the first line 4 days ahead:
refill = last_low+find(dataf(last_low:end,2)==dataf(last_low,2)+4,1)-1;
% add the amount on column 5 in the row above ind_b to 'unbl'
unbl = unbl+dataf(refill-1,5);
% add new stock to all rows below after stock arival
dataf(refill:end,5) = dataf(refill:end,5)+new_stock;
% find the next time to set a new order:
low_stock = refill+find(dataf(refill+1:end,5)<300,1);
end





share|improve this answer


























  • Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

    – Duck
    Nov 24 '18 at 22:17











  • @Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

    – EBH
    Nov 24 '18 at 22:35











  • actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

    – Duck
    Nov 24 '18 at 22:42













  • 1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

    – EBH
    Nov 24 '18 at 22:49











  • yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

    – Duck
    Nov 24 '18 at 22:57
















0












0








0







Here is a piece of code that I believe do what you need (explanations in the comments inside):



stock = 1000;
new_stock = 1200;
unbl = 0;
% culculate the comulative substraction from the initial stock:
temp = cumsum([stock; -dataf(:,4)]);
dataf(:,5) = temp(2:end); % no need for the first element (1000)
% find when to set a new order:
low_stock = find(dataf(:,5)<300,1);
% start the loop
last_low = 0;
while low_stock~=last_low % if you found a new date with low stock
last_low = low_stock; % use the new date
dataf(last_low,6) = 1; % place 1 in the date with low stock
% find the first line 4 days ahead:
refill = last_low+find(dataf(last_low:end,2)==dataf(last_low,2)+4,1)-1;
% add the amount on column 5 in the row above ind_b to 'unbl'
unbl = unbl+dataf(refill-1,5);
% add new stock to all rows below after stock arival
dataf(refill:end,5) = dataf(refill:end,5)+new_stock;
% find the next time to set a new order:
low_stock = refill+find(dataf(refill+1:end,5)<300,1);
end





share|improve this answer















Here is a piece of code that I believe do what you need (explanations in the comments inside):



stock = 1000;
new_stock = 1200;
unbl = 0;
% culculate the comulative substraction from the initial stock:
temp = cumsum([stock; -dataf(:,4)]);
dataf(:,5) = temp(2:end); % no need for the first element (1000)
% find when to set a new order:
low_stock = find(dataf(:,5)<300,1);
% start the loop
last_low = 0;
while low_stock~=last_low % if you found a new date with low stock
last_low = low_stock; % use the new date
dataf(last_low,6) = 1; % place 1 in the date with low stock
% find the first line 4 days ahead:
refill = last_low+find(dataf(last_low:end,2)==dataf(last_low,2)+4,1)-1;
% add the amount on column 5 in the row above ind_b to 'unbl'
unbl = unbl+dataf(refill-1,5);
% add new stock to all rows below after stock arival
dataf(refill:end,5) = dataf(refill:end,5)+new_stock;
% find the next time to set a new order:
low_stock = refill+find(dataf(refill+1:end,5)<300,1);
end






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 23:31

























answered Nov 24 '18 at 22:03









EBHEBH

9,48832248




9,48832248













  • Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

    – Duck
    Nov 24 '18 at 22:17











  • @Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

    – EBH
    Nov 24 '18 at 22:35











  • actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

    – Duck
    Nov 24 '18 at 22:42













  • 1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

    – EBH
    Nov 24 '18 at 22:49











  • yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

    – Duck
    Nov 24 '18 at 22:57





















  • Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

    – Duck
    Nov 24 '18 at 22:17











  • @Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

    – EBH
    Nov 24 '18 at 22:35











  • actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

    – Duck
    Nov 24 '18 at 22:42













  • 1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

    – EBH
    Nov 24 '18 at 22:49











  • yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

    – Duck
    Nov 24 '18 at 22:57



















Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

– Duck
Nov 24 '18 at 22:17





Many thanks dear @EBH but I want to iterate for all rows in this structure of data. I put different pieces of code showing the connection. Could you please help me with a way to do this for all rows?

– Duck
Nov 24 '18 at 22:17













@Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

– EBH
Nov 24 '18 at 22:35





@Duck, this will go through all the line in the file, that's why I use a while loop. I suggest that you first try it and see if you get the correct result. Then, if not, explain what was wrong, and try to explain better what the code should do, and not how it should do that.

– EBH
Nov 24 '18 at 22:35













actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

– Duck
Nov 24 '18 at 22:42







actually @EBH it saves the one in column 6 but the value in column 5 is not updated. Is there any way we can chat? It is a inventory problem so each time column 5 is less than 300 I need to know the day (1 in column 6) and in that day I ask for stock that arrives after 4 days an I add this to the actual value in that row in column 5. How could I fix this?

– Duck
Nov 24 '18 at 22:42















1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

– EBH
Nov 24 '18 at 22:49





1. Have you noticed I updated the answer? 2. Look at this line dataf(ind_b:end,5) = dataf(ind_b:end,5)+1200 this is the only case where column 5 is updated (after the first assignment) - when the stock arrives, do you see it happens at least ones?

– EBH
Nov 24 '18 at 22:49













yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

– Duck
Nov 24 '18 at 22:57







yes @EBH I see and actually is getting updated but I am getting all elements empty with your new solution. The problem is next: You have an initial stock of 1000 so orders arrive each day (column 4) and you have to reduce this stock. When the value after reducing each order is less than 300 you set a new order that arrives in 4 days (I do this by looking for the day that stock is < 300 and sum 4). In 4 days new stock arrives and you add 1200 units to the value in that day (day of arriving). Maybe it is clear in this way.

– Duck
Nov 24 '18 at 22:57






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462148%2ftranslating-individual-pieces-of-code-to-loop-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno