How can i drop a matrix column?

5 views (last 30 days)
Giovambattista
Giovambattista on 2 Oct 2014
Answered: Dan on 2 Oct 2014
Hello everybody! I'm currently attending a graduate econometric course and for a problem set I was asked to: - Build three vectors of arbitrary numbers, considering that: the first must contain a binary indicator (0-1) for gender, the second contains age, the third contains salary. Write these vectors in one Excel sheet - Import the Excel sheet in MATLAB as a matrix. - Using the functions for and if separate the matrix gender gap in two sub-matrixes: one with the earnings of men and the other with the earnings of women. - Plot the wage versus the age, using the plus sign (+) as marker style for the women, and a circle (0) as marker style for the men.
As far as the first two point, I haven't find any difficulties. However, when I'm asked to create the two sub-matrices what I was able to achieve are two matrices: one with the first column made of only zeros and the second column containing data for age and wages for woman; one with the first column made of ones, representing the mens.
This is the code I've written in Matlab:
%%Ex 3
A=xlsread('matrixA'); % import data
j=1; % index initialization
k=1; % index initialization
for i=1:size(A,1) % cycle to read the matrix
if A(i,1)==0
F(j,:) = A(i,:) % building female matrix
j = j+1;
else
M(k,:) = A(i,:) % building male matrix
k = k+1;
end
end
figure(1)
plot(F(:,2),F(:,3),'+',M(:,2),M(:,3),'ro')
xlabel('age')
ylabel('wage')
Said that, what I'm asking is: There's a way I can create the two sub-matricies without the two columns made of zeros and ones?

Accepted Answer

Dan
Dan on 2 Oct 2014
Hi, it is quite straighforward to delete columns, e.g.:
F(:,1) = []; will delete the first column of your F matrix.
Another way would be to only read the columns starting from column 2 into your F and M matrices, e.g.: F(j,:) = A(i,2:end)

More Answers (0)

Categories

Find more on Random Number Generation 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!