データをある形式から別の形式に変換することに関連するプログラミングの問題があります。
視覚化のためにマトリックスに変換する必要があるデータのテーブルを表すオブジェクトの配列があります。私はどういうわけか教育中に行列数学をカバーすることを避けたので、私の用語が間違っていたら申し訳ありません.
より具体的には、フォーマットされたデータを表すオブジェクトの配列があります。
var pokes = [
{source: "Harry", target: "Maria", type: "poke"},
{source: "Brin", target: "Serge", type: "poke"},
{source: "Maria", target: "Brin", type: "poke"},
{source: "Serge", target: "Simon", type: "poke"},
{source: "Brin", target: "Serge", type: "poke"}
];
そして、コード ダイアグラムで使用するために、それを正方行列に変換する必要があります。
var matrix = [
[0, 5, 2, 1, 4],
[7, 0, 8, 4, 7],
[9, 4, 0, 3, 1],
[8, 5, 5, 0, 8],
[6, 3, 9, 2, 0]
];
結果の行列の長さ/幅 n は、配列内の一意の個人 (Harry、Serge など) の数に等しく、対応するセル (x、y) の値は、人 x が人 y をつついた回数です。 . pokes 配列内の各オブジェクトは、1 つの pokes を表します ("type" プロパティはマトリックスの構築には影響しません)。
たとえば、オブジェクトは次のようになります。
{source: "Brin", target: "Serge", type: "poke"}
(Brin, Serge) を表すセルに 1 を追加します。
これは私の擬似コードメソッドです:
Create array, people, containing list of unique people in pokes (both source and target).
Create empty array, matrix.
For each person in people:
Create an array, foo, of size people.length.
For each poke in pokes:
If person == poke.source:
Add 1 to foo[x] where x is the index of poke.target in people.
Push foo into matrix.
このメソッドが次のとおりかどうかわからないため、実際のコードには進んでいません。
- 正しい
- 特に pokes.length が数千に近い場合、効率的/高速
アドバイスをいただければ幸いです。特に、d3.js に、私が見つけていないマトリックスを作成するための隠しメソッドがある場合。