Split a matrix into a two submatrixes by a condition
Mostra commenti meno recenti
Let "ex3" a matrix with an arbitrary length, which first column is gender (binary 0 or 1 for male o female), the second is age, the third is wage. How can I split this matrix into two submatrixes by gender using "for" and "if" functions? After that, i have to plot the wage versus the age, using plus sign (+) as a marker style for women and a circle (O) for men.
Anybody can give me an answer? I suppose that are few lines of code.
For the first part I've tried this:
for i=1:11
if ex3(i,1)==0
A=ex3(i,:)
else
B=ex3(i,:)
end
end
but this doesn't return me 2 matrixes, but a series of vectors.
2 Commenti
Iain
il 2 Ott 2014
Is there a reason you're restricting yourself to if and for?
Gino Franco
il 2 Ott 2014
Risposta accettata
Più risposte (1)
Iain
il 2 Ott 2014
This is the "simple" answer:
male = ex3(find(ex3(:,1) == 1),:);
female = ex3(find(ex3(:,1) == 0),:);
1 Commento
Mohammad Abouali
il 7 Ott 2014
Modificato: Mohammad Abouali
il 7 Ott 2014
You don't even need find . Look at the two last line of code in my answer.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!