将矩阵分为两部分 [英] Dividing a matrix into two parts
问题描述
我正在尝试对数据集进行分类.为此,我将使用数据集的第4列.如果数据集的第4列等于1,则该行将添加到名为Q1
的新矩阵中.如果数据集的第4列等于2,则该行将添加到矩阵Q2
中.
I am trying to classify my dataset. To do this, I will use the 4th column of my dataset. If the 4th column of the dataset is equal to 1, that row will added in new matrix called Q1
. If the 4th column of the dataset is equal to 2, that row will be added to matrix Q2
.
我的代码:
i = input('Enter a start row: ');
j = input('Enter a end row: ');
search = importfiledataset('search-queries-features.csv',i,j);
[n, p] = size(search);
if j>n
disp('Please enter a smaller number!');
end
for s = i:j
class_id = search(s,4);
if class_id == 1
Q1 = search(s,1:4)
elseif class_id ==2
Q2 = search(s,1:4)
end
end
这将计算Q1
和Q2
矩阵,但它们均为1x4,当给出新的Q1
时,将删除旧的.如果条件为真,我需要添加新行并将其设置为2x4.我需要扩展我的Q1
矩阵.
This calculates the Q1
and Q2
matrices, but they all are 1x4 and when it gives new Q1
the old one is deleted. I need to add new row and make it 2x4 if conditions are true. I need to expand my Q1
matrix.
简而言之,我正在尝试使用for
循环和if
语句将数据集分为两部分.
Briefly I am trying to divide my dataset into two parts using for
loops and if
statements.
数据集:
我需要如下结果:
Q1 = [30 64 1 1
30 62 3 1
30 65 0 1
31 59 2 1
31 65 4 1
33 58 10 1
33 60 0 1
34 58 30 1
34 60 1 1
34 61 10 1]
Q2 = [34 59 0 2
34 66 9 2]
如何防止我的代码删除Q1
和Q2
的前几行并获取整个矩阵?
How can I prevent my code from deleting previous rows of Q1
and Q2
and obtain the entire matrices?
推荐答案
计算中的主要问题是每次循环迭代覆盖 Q1
和Q2
.最佳解决方案:摆脱循环并使用逻辑索引.
The main problem in your calculation is that you overwrite Q1
and Q2
each loop iteration. Best solution: get rid of the loops and use logical indexing.
您可以使用逻辑索引快速确定列等于1
或2
的位置:
You can use logical indexing to quickly determine where a column is equal to 1
or 2
:
search = [
30 64 1 1
30 62 3 1
30 65 0 1
31 59 2 1
31 65 4 1
33 58 10 1
33 60 0 1
34 59 0 2
34 66 9 2
34 58 30 1
34 60 1 1
34 61 10 1
];
Q1 = search(search(:,4)==1,:) % == compares each entry in the fourth column to 1
Q2 = search(search(:,4)==2,:)
Q1 =
30 64 1 1
30 62 3 1
30 65 0 1
31 59 2 1
31 65 4 1
33 58 10 1
33 60 0 1
34 58 30 1
34 60 1 1
34 61 10 1
Q2 =
34 59 0 2
34 66 9 2
警告:解决方法慢!
如果您不愿意使用循环,请确保不要覆盖变量.每次迭代都扩展它们(这非常非常慢):
Warning: Slow solution!
If you are hell bent on using loops, make sure to not overwrite your variables. Either extend them each iteration (which is very, very slow):
Q1=[];
Q2=[];
for ii = 1:size(search,1) % loop over all rows
if search(ii,4)==1
Q1 = [Q1;search(ii,:)];
end
if search(ii,4)==2
Q2 = [Q2;search(ii,:)];
end
end
MATLAB会在Q1
和Q2
下放置橙色的摆动,因为在原地生长数组不是一个好主意.另外,您可以预分配它们search
的大小,然后去除多余的部分:
MATLAB will put orange wiggles beneath Q1
and Q2
, because it's a bad idea to grow arrays in-place. Alternatively, you can preallocate them as large as search
and strip off the excess:
Q1 = zeros(size(search)); % Initialise to be as large as search
Q2 = zeros(size(search));
Q1kk = 1; % Intialiase counters
Q2kk = 1;
for ii = 1:size(search,1) % loop over all rows
if search(ii,4)==1
Q1(Q1kk,:) = search(ii,:); % store
Q1kk = Q1kk + 1; % Increase row counter
end
if search(ii,4)==2
Q2(Q2kk,:) = search(ii,:);
Q2kk = Q2kk + 1;
end
end
Q1 = Q1(1:Q1kk-1,:); % strip off excess rows
Q2 = Q2(1:Q2kk-1,:);
这篇关于将矩阵分为两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!