I understand that you're attempting to divide M_ROCK table data into M_ORE and M_WASTE based on user input for rock types. The issue is with deleting M_WASTE rows within the loop, which can misalign indices. To fix this, accumulate row indices to delete first, then remove them after looping.
Here's a revised version of your code:
M_ROCK = rock;
M_ROCK = array2table(M_ROCK,'VariableNames',G_Value.Properties.VariableNames);
%Enter the number of rock types and then create the corresponding
%ore matrixs, after determining the multiple rock types
num=input('Please enter the number of rock types:');
%Setup initial ore and waste matrix respectively
M_ORE = zeros(R1,C1,num);
M_WASTE = zeros(R1,C1);
M_WASTE = array2table(M_WASTE,'VariableNames',M_ROCK.Properties.VariableNames);
STR = cell(1,num);
to_remove = []; % Initialize an array to keep track of rows to remove
for i = 1 : length(rock)
for j = 1 : num
STR{j} = sprintf('r%d_mill_tonnage',j);
if table2array(M_ROCK(i,STR{j})) > 0
M_ORE(i,:,j) = table2array(M_ROCK(i,:));
else
M_WASTE(i,:) = M_ROCK(i,:);
if table2array(M_WASTE(i,STR{j})) > 0
to_remove = [to_remove; i]; % Mark row for removal
end
end
end
end
M_WASTE(to_remove, :) = []; % Remove all marked rows at once
Refer to the following MathWorks Documentation :
- https://www.mathworks.com/help/matlab/tables.html
- https://www.mathworks.com/help/matlab/if.html
- https://www.mathworks.com/help/matlab/ref/for.html
Hope it Helps!