MATLAB Answers

How to find the position of a real vector in a matrix?

1 view (last 30 days)
Cantor Set
Cantor Set on 6 Mar 2020
Edited: dpb on 7 Mar 2020
I do have a very big matrix of real numbers, I need a function that takes a vector as input and returns its position in the matrix.
An example:
A =[
0.7081 0.0797 0.1418 0.5849 0.1305
0.0738 0.4849 0.5060 0.8479 0.5003
0.8723 0.1511 0.6346 0.0683 0.7673
0.2021 0.4461 0.7313 0.7355 0.4157
0.7050 0.8435 0.8158 0.2139 0.8008]
Suppose I want to find the position of
v=[ 0.7081 0.0797 0.1418 0.5849 0.1305]
in A
How can I do it?
I edited the question

  2 Comments

Walter Roberson
Walter Roberson on 6 Mar 2020
You cannot. Those values do not exist in the matrix.
You need to give the command
format long g
And then display A again.

Sign in to comment.

Answers (2)

dpb
dpb on 6 Mar 2020
>> find(ismember(A,v,'rows'))
ans =
1
>>
works here, but in general for floating point values
>> find(ismembertol(A,v,2*eps,'byrows',1))
ans =
1
>>
is more robust and will avoid surprises.

  2 Comments

Cantor Set
Cantor Set on 6 Mar 2020
I tried it on a very big matrix of real numbers it returned
ans =
Empty matrix: 0-by-1
dpb
dpb on 7 Mar 2020
>> find(ismembertol(w,c,2*eps,'byrows',1))
ans =
1
>>

Sign in to comment.


Walter Roberson
Walter Roberson on 6 Mar 2020
Dists = pdist2(A, v);
[mindist, rowidx] = min(Dists) ;
if mindist > some_tolerance
error('row not found') ;
end

  3 Comments

Cantor Set
Cantor Set on 7 Mar 2020
w =[
472.1745 237.0587 217.2062
482.5531 230.1160 214.5375
511.5849 221.5821 203.8826
528.9620 210.4759 200.9207
550.9417 196.1739 193.1201
559.0008 207.2335 186.1113
579.9176 188.3211 185.3489
600.6789 178.9065 181.5854
602.5532 159.9476 178.6138
610.0186 163.3685 171.5659
627.4484 155.7319 176.9325
632.2046 152.3424 175.2902
637.9136 182.4038 149.4447
653.2446 145.8217 169.4935
653.4360 145.6781 169.4251
653.6974 145.5827 169.3469
654.4806 145.2974 169.1123
654.7265 155.4882 168.9364
655.5309 155.2029 168.7018
655.7820 144.8259 168.7213
657.4487 168.3275 149.6988
657.9721 142.2886 167.7829
658.2313 142.1932 167.7047
658.5682 153.4142 163.8239
659.0075 141.9079 167.4701
660.2975 141.4364 167.0791
661.7071 142.7182 166.9227
664.5972 140.9109 165.8474
666.1699 139.3287 165.2804
667.4011 141.2962 165.2218
670.1464 151.4256 163.8435
671.8341 139.3555 163.7848
672.0185 139.2119 163.7164
674.3094 148.3308 161.7418
674.6270 148.1336 161.5659
674.8512 147.9573 161.4975
675.4193 147.8472 161.3313
675.6830 147.7521 161.2531
675.9466 147.6572 161.1749
676.3873 135.8224 162.0742
678.8491 123.5834 142.7682
680.1346 119.4330 154.2834
684.0921 117.2617 152.1622
697.7402 114.0022 151.0869
699.3930 113.3286 150.5395
699.6284 113.2331 150.4613
700.3333 112.9479 150.2267
702.9594 126.3037 141.0282
703.0034 109.2507 146.7760
703.1357 111.8242 149.2883
704.6020 108.5772 146.2286
704.8296 108.4817 146.1504
705.2194 110.9995 148.5845
705.5113 108.1965 145.9158
706.6765 107.0795 144.6548
708.2208 107.0728 144.9774
708.2543 106.4059 144.1074
708.4790 106.3105 144.0292
709.1518 106.0252 143.7946
710.2349 106.2481 144.2736
711.8258 104.9015 142.8562
712.4363 119.5465 136.8249
713.8132 104.0768 142.1524
716.8004 118.1622 134.5669
717.6171 106.2958 144.2834
720.0016 102.3000 141.9667
720.2272 102.2049 141.8885
720.2637 99.9861 136.6783
720.4527 102.1100 141.8103
721.1277 101.8265 141.5757
721.2040 99.5198 135.9940
721.6670 99.2938 135.6519
721.7590 99.2492 135.5835
721.8508 99.2048 135.5151
722.2068 101.5444 139.9725
722.5785 98.8562 134.9676
722.7751 99.5114 128.6627
724.8787 99.0867 126.2677
725.6222 99.3731 137.8513
725.8663 98.6094 125.8767
726.0632 98.5145 125.7985
727.1261 99.3523 139.4642
729.0721 104.0235 142.0351
729.2169 97.7742 138.3890
730.5481 96.5638 124.4887
731.5295 96.0865 124.0977
731.7252 95.9916 124.0195
731.9624 95.8937 123.9608
732.1112 96.5233 137.3723
732.3325 96.4285 137.2941
733.1793 100.9457 138.5747
733.5947 94.9630 122.7487
735.1199 96.1880 136.5708
735.6291 95.0281 136.1211
736.2833 94.7532 135.8865
737.0511 93.4551 123.1201
737.5205 93.3695 122.5727
738.0311 92.9778 122.7291
738.2265 92.8829 122.6509
738.4966 92.8922 122.1817
738.6912 92.7973 122.1035
739.2082 92.4401 120.9696
739.5890 92.2241 122.1035
740.0484 92.1385 121.5561
744.2080 90.2671 131.7614
744.4229 90.1722 131.6832
744.8522 89.9830 131.5268
748.7793 92.3544 130.0410];
________________________________________________
for example
c=[472.1745 237.0587 217.2062]; % which is the first row in w
when I type your code
Dists = pdist2(w, c);
[mindist, rowidx] = min(Dists) ;
if mindist > 1e-10
error('row not found') ;
end
It tells me row not found
dpb
dpb on 7 Mar 2020
Shouldn't w/ the given example data...altho suppose it's possible with the real data instead of that rounded off in the pasted text instead of actual data from memory.
>> D=pdist2(w,c);
>> [minD,ixMin]=min(D)
minD =
0
ixMin =
1
>>
With the rounded data from the posting it's identically zero.
Did you look at what was returned at all or just complain...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!