Pseudo Element by Element Vector Subtraction
    조회 수: 7 (최근 30일)
  
       이전 댓글 표시
    
I am trying to subtract vectors stored in two different arrays. I want to subtract every row vector in matrix B from every row vector in matrix A such that the results of row vector 1 of A minus each of the row vectors of B is stored in a an array, the results of row vector 2 of A minus all row vectors B is stored in a different array...and so on until the end. Matrix A is a 63x2 array while matrix B is a 61x2 array. My final goal is to comapre all the results and identify and store all vectors that have duplicates.
A = [0	0
-5.22988566179964	6.05378360734033
5.82743694158990	-5.48096512411759
-7.71867012579003	13.6568061570767
-6.41307857048505	-4.78251223195356
-12.8832160382871	3.72429135615449
13.0437173772689	-8.93426740941455
-3.95560029264112	20.7165020025679
-14.9311651399118	17.1180076350708
-12.8261571409701	-9.56502446390712
-20.5365464147745	1.39479910496865
20.2599978129478	-12.3875696947115
-1.17023252546704	28.2159504090567
-22.1436601540336	20.5792091130649
-17.3827161983918	-16.1405677537219
-28.1898767912619	-0.934693146217198
23.2616199344730	-19.8031117948934
4.31055626391175	34.0435531963286
27.4257074918059	-8.83051968124259
-29.3561551681553	24.0404105910590
-19.3637625207825	-23.8914035453345
-35.8432071677493	-3.26418539740304
23.0577732626258	-27.8005142815998
9.79134505329053	39.8711559836006
10.4567903913941	28.9226347191244
34.5914171706640	-5.27346966777369
-31.5385456076644	31.7369794350011
-21.3448088431732	-31.6422393369470
-43.4965375442368	-5.59367764858889
25.6191596676504	-35.3793862084867
16.3972779876710	44.3835444937432
-35.1703582141579	38.8650883841765
-23.3258551655640	-39.3930751285596
-51.1498679207242	-7.92316989977473
28.1805460726749	-42.9582581353737
23.0032109220515	48.8959330038858
37.7741257337733	2.06617379783899
-29.3310994911081	-31.1740922588744
17.7048992093770	-36.5474963910512
-38.8021708206513	45.9931973333520
-22.3878218988045	-47.3378906450113
35.8328768192608	-45.2910320517349
29.7346992697278	53.2187867632789
18.1810613102127	55.2792607505272
40.9568342968826	9.40581726345168
-42.4339834271448	53.1213062825274
-22.9362553876826	-55.3190698002663
43.4852075658468	-47.6238059680961
36.8803922974807	49.6216947220428
-46.6615356421781	1.75362029283338
-32.2411590073453	50.5706555784546
-30.3866778744434	-47.4731795247682
32.3995579608740	-52.5168414608354
13.3589116983739	61.6625884971687
48.2086638919891	6.02780490060624
-49.3011842938828	49.0174602503434
-17.7011930022771	-61.3683773491444
46.9026731045917	-40.3904852029502
44.0260853252335	46.0246026808068
-49.8265337401194	9.10091823425565
-38.3629147966204	-48.0893325587636
55.4604934870957	2.64979253776079
51.1717783529864	42.4275106395707];
B = [5.82743694158990	-5.48096512411759
-2.48878446399039	7.60302254973639
-6.41307857048505	-4.78251223195356
-7.65333037648742	-2.32949225118584
7.21628043567895	-3.45330228529695
3.76306983314891	7.05969584549112
-7.21249501412177	3.46120147799410
-6.41307857048505	-4.78251223195356
-7.65333037648742	-2.32949225118584
7.21628043567895	-3.45330228529695
2.78536776717408	7.49944840648882
-7.21249501412177	3.46120147799410
-4.55655905742170	-6.57554328981479
-7.65333037648742	-2.32949225118584
3.00162212152522	-7.41554210018192
5.48078878937879	5.82760278727195
7.16570967885811	3.55705001346891
-7.21249501412177	3.46120147799410
-1.98104632239072	-7.75083579161255
-7.65333037648742	-2.32949225118584
-0.203846671847199	-7.99740248670635
5.48078878937879	5.82760278727195
6.14623412748235	-5.12091847720419
7.16570967885811	3.55705001346891
-2.18239043950905	7.69656884394205
-1.98104632239072	-7.75083579161255
-7.65333037648742	-2.32949225118584
2.56138640502452	-7.57887192688698
6.60593293438051	4.51238851014260
-3.63181260649347	7.12810894917545
-1.98104632239072	-7.75083579161255
-7.65333037648742	-2.32949225118584
2.56138640502452	-7.57887192688698
6.60593293438051	4.51238851014260
3.18270856310927	7.33964346561268
-7.98629064793488	0.468147078072581
-7.91426045827336	-1.16811018256442
-3.63181260649347	7.12810894917545
0.938033266759421	-7.94481551645176
7.65233074658597	-2.33277391636118
6.73148834767623	4.32285375939311
-4.82214961183880	6.38332774664147
3.18270856310927	7.33964346561268
-3.63181260649347	7.12810894917545
-0.548433488878012	-7.98117915525501
7.65233074658597	-2.33277391636118
7.14569302775288	-3.59709204123606
-3.16499809794131	7.34729794142227
6.56101181330606	4.57745824510266
-7.99885597563890	-0.135288879756905
-3.43331885838680	-7.22580940910052
-4.82214961183880	6.38332774664147
7.25182959510656	-3.37801236284544
-6.86720086673803	-4.10384603218402
5.23506238540546	-6.04930754887804
3.41746553874489	7.23332076514592
7.14569302775288	-3.59709204123606
-3.16499809794131	7.34729794142227
-7.97623692217691	-0.616153033995415
7.25182959510656	-3.37801236284544
7.14569302775288	-3.59709204123606];
SizeA = size(A,1);
SizeB = size(B,1);
for i = 1:SizeA
    for j = 1:SizeB
        vectortest(i,:) = vein_node_array(i,:)-Btest(j,:);
    end
end
댓글 수: 0
채택된 답변
  Star Strider
      
      
 2020년 10월 26일
        See if this does what you want: 
C1 = A(:,1).' - B(:,1);                                             % Col #1 In ‘A’ - Col #1 In ‘B’
C2 = A(:,2).' - B(:,2);                                             % Col #2 In ‘A’ - Col #2 In ‘B’
[Dupr,Dupc] = find((C1 == 0) & (C2 == 0));
Dupt = array2table([Dupc,Dupr], 'VariableNames',{'RowA','RowB'})
producing: 
Dupt =
  3×2 table
    RowA    RowB
    ____    ____
     3       1  
     5       3  
     5       8 
This works by creating two matrices formed by subtracting column vectors in ‘B’ from row vectors in ‘A’ (created by the transposition) for each column of both matrices.  The elements of ‘C1’ and ‘C2’ are both equal to 0 if the respective rows of the ‘A’ and ‘B’ are duplicates.  
In ‘Dupt’, the respective rows of matrix ‘A’ and matrix ‘B’ are duplicates, as demonstrated by: 
A_dup = A(Dupc,:)
B_dup = B(Dupr,:)
producing: 
A_dup =
   5.827436941589900  -5.480965124117590
  -6.413078570485050  -4.782512231953560
  -6.413078570485050  -4.782512231953560
B_dup =
   5.827436941589900  -5.480965124117590
  -6.413078570485050  -4.782512231953560
  -6.413078570485050  -4.782512231953560
These are by design exact matches.  It might be possible to use a tolerance to find near matches, however I will defer that for the time being.  
댓글 수: 9
  Star Strider
      
      
 2020년 10월 27일
				O.K.  
I’m beginning to understand what you’re doing.  I need to think about this a bit.  
  Star Strider
      
      
 2020년 10월 29일
				My apologies for the delay.  It took me a while to figure out an acceptable approach to this problem.  
This seems to work, at least with the ‘A’ matrix to identify an approximate graph of the connections between the stars: 
Mdl = ExhaustiveSearcher(A);
[Idx,D] = rangesearch(Mdl, A, 9);
figure
hold on
for k = 1:numel(Idx)
    xv = [repmat(A(Idx{k}(1),1),numel(Idx{k}),1), A(Idx{k}(1,:),1)];
    yv = [repmat(A(Idx{k}(1),2),numel(Idx{k}),1), A(Idx{k}(1,:),2)];
    plot(xv.', yv.','-p')
end
hold off
grid
axis('equal')
producing: 

It works by using a 
-nearest-neighbour ExhaustiveSearcher exhaustive search, then uses the rangesearch function to identify the elements within a given distance (I empirically chose 9), returning the indices (in ‘Idx’ and the distances (in ‘D’ that I returned simply for informaiton).  I then plotted them in the loop.  The ‘xv’ and ‘yv’ matrices use the ‘Idx’ cell array elements to index into ‘A’ to plot each segment.  Those take the ‘x’ vectors of each ‘Idx’ element for the first and subsequent columns of the indexed elements of ‘A’ and do the same for the ‘y’ vectors, so that the first value is replicated in the first column and the second column are the referenced neighbours.  (This is a bit difficult to describe.  Looking at the ‘xv’ and ‘yv’ matrices will help in understanding them.)  They are then transposed so that they plot individual lines rather than one continuous line.  
I did not try it on ‘B’.  
I am not certain what you are doing, however this should make it easier for you to identiffy the branch-points and the properties of the stars connected to them.  
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!