2012-02-22 60 views
2

從結構體變量值I具有以下製表符分隔txt文件:建立一個矩陣,在MATLAB

User Item Rate 
1  1  9 
1  2  8 
2  2  7 
3  1  6 
3  2  8 
3  3  5  

我使用 tdfread這在相應的變量使上述每一列導入此Matlab的一個結構體(例如,struct.user,struct.item,struct.rate)。從那裏,我想建立下面的矩陣而不使用循環:

9  8  NaN 
NaN  7  NaN 
6  8  5 

其中每一行代表上述(1〜3)中的一個用戶,每一列代表的項目之一。這可能嗎?

感謝,

回答

2

嘗試是這樣的:

i = struct.User; 
j = struct.Item; 
A = nan(3,3); 
A(sub2ind(size(A),i,j)) = struct.rate; 
+1

你並不需要調用的。你可以這樣做:'A = nan(3,3);' – gnovice 2012-02-22 17:50:06

+0

謝謝,我編輯了我的答案。 – 2012-02-22 18:01:04

+0

就是這樣!非常感謝。 – 2012-02-22 19:00:42