How to divide the original matrix into different matrices according to the strings

1 回表示 (過去 30 日間)
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.
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 件のコメント
Chao Zhang
Chao Zhang 2021 年 5 月 30 日
Thanks! But can my idea be realized with matlab?
Walter Roberson
Walter Roberson 2021 年 5 月 30 日
STR = cell(1,num);
That should not be within the for j loop.

サインインしてコメントする。

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 31 日
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
  1 件のコメント
Chao Zhang
Chao Zhang 2021 年 5 月 31 日
Thanks for your answer! But this line ' if table2array(M_ROCK(i,STR{j}))>0' has an error Row index exceeds table dimensions

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by