How to divide the original matrix into different matrices according to the strings
2 views (last 30 days)
Show older comments
Chao Zhang
on 30 May 2021
Commented: Chao Zhang
on 31 May 2021
My idea is create the 'M_ROCK' matrix if the rock_index ==2 (i.e. there are multiple rock types in the model). And then input the number of rock types according to the 'M_ROCK'. The following picture is the screenshot of part M_ROCK.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/636065/image.png)
For example, there are two rock types r1 and r2 in 'M_ROCK', the num then will be input 2, and the values in the 'M_ROCK' will be transferred to the corresponding new matrix M_ORE(this might include M_ORE1 and M_ORE2) and M_WASTE, according to the strings 'r1_mill_tonnage' and 'r2_mill_tonnage'. However, when I run the following code, there is an error 'Row index exceeds table dimensions'.
In this case, there are only two types of rocks, there may be three, four, etc. other situations, how to write a code, can summarize these situations. In other words, input the number of rock types, the values will automically transfer into the corresponding M_ORE (ORE1,ORE2,...) and M_WASTE according to the strings 'rn_mill_tonnage'(n=1,2,3,....)
Thanks in advance for your help!
elseif rock_index == 2
%%
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:');
%6.Extract ore and waste matrix from ROCK matrix(multiple rock types)
%Setup initial ore and waste matrix respectively
M_WASTE = zeros(R1,C1);%waste matrix for multiple rock types
M_ORE = zeros(R1,C1);
for j = 1 : num
STR = cell(1,num);
STR{j} = sprintf('r%d_mill_tonnage',j);
if table2array(M_ROCK(i,STR{j}))>0
M_ORE(i,:) = table2array(M_ROCK(i,:));
for k = 1:num
M_ORE = zeros(R1,C1,num);
end
else
M_WASTE(i,:)=table2array(M_ROCK(i,:));
end
end
3 Comments
Walter Roberson
on 30 May 2021
STR = cell(1,num);
That should not be within the for j loop.
Accepted Answer
Sulaymon Eshkabilov
on 31 May 2021
Hi,
Here are some corrections made in your code:
..
STR = cell(1,num);
M_ORE = zeros(R1,C1,num);
for j = 1 : num
STR{j} = sprintf('r%d_mill_tonnage',j);
if table2array(M_ROCK(i,STR{j}))>0
M_ORE(j,:) = table2array(M_ROCK(j,:)); % not "i" but "j"
else
M_WASTE(j,:)=table2array(M_ROCK(j,:)); % not "i" but "j"
end
end
More Answers (0)
See Also
Categories
Find more on Calendar 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!