How to divide a dataset into two subsamples?

9 views (last 30 days)
IvyR
IvyR on 14 Feb 2023
Edited: Antonios Dougalis on 14 Feb 2023
Hello,
Im trying to partition a datset into two subsamples. Unfortunately, I'm struggling to code it.
How do I divide a datset into two subsamples, one corresponding to those Losses that involved a LAWYER (=1) and the other to those in which a LAWYER was not involved (=2).
1 Lawyer LOSS
1 1 612
2 2 453
3 1 188
4 2 811
5 1 695
6 2 215
7 1 684
8 1 354
9 1 743
10 2 246
I tried to write the code in Matlab like this;
%Claims that a Lawyer is involved (=1)
LOSS_1=LOSS;
LOSS_1=LOSS(ATTORNEY(:,2)==1)
When I try to run it, I keep getting errors. What is the correct way of coding this. Thank you.

Answers (2)

Antonios Dougalis
Antonios Dougalis on 14 Feb 2023
Edited: Antonios Dougalis on 14 Feb 2023
% Suppose that you have an array A with two columns (lawyer, loss)
A = [ [1;2;1;2;1;2;1;1;1;2], [612;453;188;811;695;215;684;354;743;246] ];
lawyer1 = find(A(:,1)==1); % indexes of the rows that are 1 corresponding to lawyer 1
lawyer2 = find(A(:,1)==2); % indexes of the rows that are 2 corresponding to lawyer 2
% use the indexes to extract the losses for each lawyer from the second
% column of A
loss_lawyer1 = A(lawyer1,2);
loss_lawyer2 = A(lawyer2,2);

Cameron
Cameron on 14 Feb 2023
Try deleting this line
LOSS_1=LOSS;
If that doesn't work, paste your data for LOSS and ATTORNEY.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!