extract the information from a matrix with three columns

2 vues (au cours des 30 derniers jours)
Ahmed
Ahmed le 15 Sep 2016
Commenté : Ahmed le 16 Sep 2016
I have a matrix with three columns see the attachment i.e.
E1 E2 W
6 1464 0.36
6 1534 0.27
6 1585 0.27
8 1331 0.332
11 445 0.39
13 844 0.286
14 12 0.126
18 952 0.31
19 2376 0.32
20 394 0.22
20 399 0.22
20 589 0.22
394 8 0.236
399 8 0.236
1187 9 0.350
394 10 0.236
445 11 0.390
14 12 0.126
I used this condition
for k=1:size(y,1)
G(y(k,1),y(k,2))=true;
G(y(k,2),y(k,1))=true;
end
B=cellfun(@(x1) find(x1),num2cell(G,2),'un',0);
to extract links information like this:
1 394
2 2378
3 282
4 282
5 536
6 [1464,1534,1585]
7 2087
8 [394,399,1331]
9 1187
I need a third column contains the weight
e.i. {6,[1464,1534,1585],[0.36;0.27;0.27]}
I tried to use the above condition but I did not get the right values. Does anyone have idea how to do that ??

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 15 Sep 2016
Modifié(e) : Andrei Bobrov le 16 Sep 2016
EDIT
load('y.mat');
[yy,i0] = unique([y(:,1:2);y(:,[2 1])],'rows');
y3 = repmat(y(:,3),2,1);
y3 = y3(i0);
[a,~,c] = unique(yy(:,1));
[ii,jj] = ndgrid( c,1:2);
B = [num2cell(a), accumarray([ii(:),jj(:)],[yy(:,2);y3],[],@(x){x})];
  3 commentaires
Andrei Bobrov
Andrei Bobrov le 16 Sep 2016
corrected
Ahmed
Ahmed le 16 Sep 2016
Thanks a lot

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by