add the value of column from text file to the value to matrix
1 回表示 (過去 30 日間)
古いコメントを表示
i create a matrix having userid as a row and itemid as a column ,now i want to add the value of the third column ,in the index value of userid,itemid.Initially fill the matrix by zeros,but now want the value associate with it.
data=load('u.data');
uni_user=unique(data(:,1));
uni_item=unique(data(:,2));
rating_matrix=zeros(length(uni_user(:,1)),length(uni_item(:,1)));
example this is my dataset
196 242 3
186 302 3
22 377 1
196 51 2
now i want a matrix like
5 件のコメント
Walter Roberson
2019 年 9 月 14 日
sparse() does not require square matrices.
>> full(sparse(1:5,[1 2 3 1 2],1))
ans =
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
採用された回答
Walter Roberson
2019 年 9 月 14 日
編集済み: Walter Roberson
2019 年 9 月 14 日
data = load('u.data');
[uni_user, ~, uni_user_idx] = unique(data(:,1));
[uni_item, ~, uni_item_idx] = unique(data(:,2));
rating = data(:,3);
rating_matrix = sparse(uni_user_idx, uni_item_idx, rating);
This will have about 0.6% occupancy and so is well suited for a sparse matrix, but if you have particular reason, you can full() to turn it into a 12 megabyte rectangular array.
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!