Append new column to a cell with zero & one values
2 views (last 30 days)
Show older comments
I have a cell type variable with thousand rows and 8 columns. For example:
c3
A={ 12 0 69.11 13 40 100 90602 1996
13 0 46.21 16 48 183 30502 1996
16 0 45.34 11 37 183 40701 1996
18 0 8.70 17 81 76 60110 1996
26 0 78.23 13 48 254 40301 1996
41 0 17.83 26 41 98 20501 1996
42 0 92.02 12 75 7 20502 1996};
Taking into account the third column (c3) I would like to append a new column to variable A that wil have the value of 1 if the value in c3 corresponds to the bottom 5th percentile of all the values in c3 and zero otherwise. Basiscally append dummy variable that gives 1 for 'scores' in the bottom 5% of the distribution (c3).
Can someone help me? Thank you.
2 Comments
the cyclist
on 17 Aug 2014
Is the notation in c3 such that the comma represents the decimal? "69,11" is a single value, equal to 69 + 11/100?
Accepted Answer
Adam
on 17 Aug 2014
pVal = prctile( [A{:,3}], 20 );
A(:,end+1) = {0}
A( [A{:,3}] <= pVal, end ) = {1}
I think that should do what you want.
0 Comments
More Answers (2)
the cyclist
on 17 Aug 2014
A = { 12 0 69.11 13 40 100 90602 1996
13 0 46.21 16 48 183 30502 1996
16 0 45.34 11 37 183 40701 1996
18 0 8.70 17 81 76 60110 1996
26 0 78.23 13 48 254 40301 1996
41 0 17.83 26 41 98 20501 1996
42 0 92.02 12 75 7 20502 1996};
c3 = [A{:,3}]';
p5 = prctile(c3,5);
bot = c3 <= p5;
A = [A,num2cell(bot)];
0 Comments
Guillaume
on 17 Aug 2014
sortedcolumn = sort([A{:, 3}]);
index = floor(0.05 * numel(sortedcolumn)); %could use round as well
threshold = sortedcolumn(index);
Gets you the max of your 5 percentile.
inbottom = num2cell(A{:, 3} < threshold); tells which rows is in bottom percentile
[A{:, 9}] = inbottom{:}; %puts it in new 9th columnn
To insert in new column.
Note, this uses expansion of cell arrays to comma-separated lists. Search for comma-separated list in the matlab doc for more information.
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!