extract the information from a matrix with three columns

조회 수: 2 (최근 30일)
Ahmed
Ahmed 2016년 9월 15일
댓글: Ahmed 2016년 9월 16일
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 ??

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 9월 15일
편집: Andrei Bobrov 2016년 9월 16일
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
Ahmed
Ahmed 2016년 9월 16일
Thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by