MATLAB Answers

How to extract the column first of matrix based on the values of the second column in the matrix??

1 view (last 30 days)
Mohammad Bhat
Mohammad Bhat on 15 Feb 2018
Edited: Birdman on 15 Feb 2018
Hi, I have a matrix (100 x2), I have to extract the sub-matrix on the basis of values in the second column e.g v =
31.1302569857687 -1
32.180318519244 -1
12.7872162725122 -1
13.2835575054275 -1
10.021621625266 -1
36.6009958880903 0
36.9774647589583 0
36.787129542817 0
38.7367125605671 0
40.100285535143 0
42.8454536678047 0
26.452464913501 0
19.5200640367802 0
19.245074694581 0
14.6551322068414 0
19.7294931511177 0
18.3170112190827 0
16.904818839609 0
16.1732155120743 0
13.5931195830832 0
11.4443392120297 0
15.1648574012419 0
19.0397715322427 0
22.1538461672009 0
21.4124473145878 0
27.5084150761181 0
32.1131265995698 0
36.8482414777151 0
41.5676905781401 0
46.4802420389567 0
51.5351617830002 0
51.4051835907625 0
52.6130487616903 0
45.4204018035948 1
58.9232797797271 1
55.494620460005 1
52.3197180802802 1
49.4474761742195 1
46.9334944362765 1
61.1522109166954 1
43.9118765256052 1
42.2508331278804 1
40.3429411421627 1
65.5795158567064 1
38.8385491490092 1
37.8963441508545 1
34.9973270407898 1
29.0715135484894 1
26.3543715538808 1
38.95578134244 1
23.0896708508372 1
22.7642900174813 1
35.1498634421245 1
31.3562258570766 1
24.5795219644321 1
24.7562699128928 1
21.6714766455819 1
21.45630210451 1
20.6129304078775 1
14.1022303200593 1
16.5497099672472 1
13.1488744765474 1
14.6932943889381 1
20.2729598233706 1
25.23 1
22.6374225564661 1
23.8778746960445 1
26.0356083086223 1
27.6852469737945 1
32.3693203512215 1
35.8534921590631 1
39.6632436898446 1
44.2794862210482 1
44.7746903953562 1
47.3547558329678 1
45.1999214601088 1
52.9772866424848 1
56.3786564224441 1
57.0621845007707 1
44.4008209383565 1
45.6003607441871 1
49.3203092042213 1
47.3169409408512 1
48.544751518573 1
53.2521633363378 1
56.5622922095631 1
59.3042401519487 1
51.4156872948325 1
66.4488743320758 2
62.2216433405612 2
58.6323536965727 2
51.0483388564212 2
47.0732716092689 2
11.0405117635008 2
13.4057040098609 2
12.9604359494579 2
18.4177333024452 2
23.171381055086 2
16.5587710896673 2
19.2045020763362 2
I have to extract the values or sub-matrix which are having specific values of second column e.g above I should get separate three sub matrices such as
31.1302569857687 -1
32.180318519244 -1
12.7872162725122 -1
13.2835575054275 -1
10.021621625266 -1
similarly for ther values of second column in matrix,,,,???please help

Accepted Answer

Birdman
Birdman on 15 Feb 2018
[r,~]=find(A==-1);
A=A(r,:)
  2 Comments
Birdman
Birdman on 15 Feb 2018
Then you will change it to
[r,~]=find(A==0);
A=A(r,:)
or
[r,~]=find(A==1);
A=A(r,:)
and else. But if you want to automatically divide them according to the changes in second column, do this:
[~,idx,~]=unique(A(:,2));
idx=[idx.' size(A,1)];
for i=1:numel(idx)-1
B{i,1}=A(idx(i):idx(i+1)-1,:);
end
and you will store them in cell array since they have different dimensions.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!