Split a table into groups and call a method with multiple parameters
显示 更早的评论
I'm quite new to Matlab so this should be a fairly simple question.
I have the following table:
label team id
lab1 team1 403
lab2 team1 932
lab3 team1 264
lab4 team1 552
lab5 team2 374
lab6 team2 765
lab7 team2 639
lab8 team2 707
lab9 team3 1295
lab10 team3 274
lab11 team3 270
lab12 team3 349
lab13 team3 1280
lab14 team4 481
lab15 team4 1191
lab16 team4 236
lab17 team4 742
lab18 team4 1340
Also, I have a method like this:
CalculateGroupCentrality(A, L)
Where A is an adjacency matrix and L a line vector. What I would like to do is to split this table into line vectors - e.g., team1 - 403, 932, 264, 552 - and call the method CalculateGroupCentrality for each of the teams. Also, A (the adjacency matrix) should be the same for each call.
I tried something like this:
graph = read_gml('mygraph.gml');
callCalculateGroupCentrality = @(L)CalculateGroupCentrality(get_adjacency_matrix(graph),L);
G = findgroups(teamnetworkid.team);
centralities = splitapply(callCalculateGroupCentrality, teamnetworkid.id, G)
But that returns the following error:
Error using splitapply (line 132)
Applying the function '@(L)CalculateGroupCentrality(get_adjacency_matrix(graph), L)' to the 1st group of data generated the following error:
Subscripted assignment dimension mismatch.
I understand this might be simple, but not sure what I'm missing here.
采纳的回答
更多回答(1 个)
Guillaume
2018-1-10
The error is generated by CalculateGroupCentrality. You should get the same error if you called CalculateGroupCentrality with, for example:
CalculateGroupCentrality(get_adjacency_matrix(graph), [403; 932; 264; 552])
Is the function designed to work with vectors in any direction or is it restricted to row vectors?
To narrow the problem down, you would need to show us the code of that function.
6 个评论
Srecko Joksimovic
2018-1-10
Guillaume
2018-1-10
What is returned by CalculateGroupCentrality in the above case? A scalar value?
Can you share the code of that function?
The way you're using splitapply is correct as long as the function does not error and returns scalar values (regardless of input).
Can you also show the output of
teamnetworkid.id(G == 1)
Srecko Joksimovic
2018-1-10
Guillaume
2018-1-10
Could it be that when the anonymous function was created get_adjacency_matrix was a variable rather than a function? Or a different function?
Does callCalculateGroupCentrality(teamnetwork.id(G == 1)) work?
Srecko Joksimovic
2018-1-11
Srecko Joksimovic
2018-1-11
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!