How to divide a dataset into two subsamples?

조회 수: 6 (최근 30일)
IvyR
IvyR 2023년 2월 14일
편집: Antonios Dougalis 2023년 2월 14일
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