Dividing a matrix into two parts

1 visualizzazione (ultimi 30 giorni)
betul uslu
betul uslu il 18 Dic 2019
Risposto: Xin Li il 19 Dic 2019
Without using
Q1 = search(search(:,4)==1,:) % == compares each entry in the fourth column to 1
Q2 = search(search(:,4)==2,:)
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.
My code:
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
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 Q1matrix.
Briefly I am trying to divide my dataset into two parts using for loops and if statements.
Dataset:
I need outcome like:
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;]
How can I prevent my code from deleting previous rows of Q1 and Q2 and obtain the entire matrices?

Risposte (1)

Xin Li
Xin Li il 19 Dic 2019
Hi, Betul
From your code, I noticed that you do a value assignment to Q1 and Q2 in the for loop. The assignment will overwrite the previous data saved in Q1 and Q2 for sure.
If you want to append the new results to the back of Q1 and Q2.
You may try this:
Q1 = [];
Q2 = [];
for ...
...
Q1 = [Q1; search(s,1:4)];
...
end

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by