2

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 Answer 1

2

Try something like this:

i = struct.User;
j = struct.Item;
A = nan(3,3);
A(sub2ind(size(A),i,j)) = struct.rate;
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need the call to ONES. You can do this instead: A = nan(3,3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.