How to divide a dataset into two subsamples?

6 次查看(过去 30 天)
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.

回答(2 个)

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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by