How to divide a dataset into two subsamples?

2 visualizzazioni (ultimi 30 giorni)
IvyR
IvyR il 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.

Risposte (2)

Antonios Dougalis
Antonios Dougalis il 14 Feb 2023
Modificato: Antonios Dougalis il 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 il 14 Feb 2023
Try deleting this line
LOSS_1=LOSS;
If that doesn't work, paste your data for LOSS and ATTORNEY.

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by