2
votes

I have the following tab-separated txt file:

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

I import this to Matlab using tdfread which puts each column above in a corresponding variable of one struct (e.g., struct.user, struct.item, struct.rate). From there, I would like to build the matrix below without the use of loops:

9       8       NaN
NaN     7       NaN
6       8       5

Where each row represents one of the users above (from 1 to 3) and each column represents one of the items. Is this possible?

Thanks,

1

1 Answers

2
votes

Try something like this:

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