# convert matrix to tensor

116 views (last 30 days)
SOUMYA SUVRA GHOSAL on 4 Jun 2018
Commented: Aakash Deep on 5 Jun 2018
i have a data set which has the following structure road_segment_id/<time_slot>/<driver_id>/<travel_time> Now I need to select randomly 100 driver ids and create a 3-d tensor with road_segment_id,driver_id,time-slot on 3 sides.Let the tensor be A then a(i,j,k)=c represents that time taken by driver j to traverse road segment i on time-slot k is c. the attached file showing a part of data set is there.

#### 1 Comment

SOUMYA SUVRA GHOSAL on 4 Jun 2018
here's my code,please tell me where is the error
if
time_slot=unique(M(:,2));
user_id=unique(M(:,3));
n_time=numel(time_slot);
n_user=numel(user_id);
driver=intersect(user_id,randsample(user_id,100));
for jj=1:100
for kk=1:n_time
A(ii,jj,kk)=M(r,4);
end
end
end
A is the 3d array
its giving error that
"Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
Error in tensor A(ii,jj,kk)=M(r,4);

Aakash Deep on 4 Jun 2018

Show 1 older comment
Aakash Deep on 4 Jun 2018
Hey Soumya, yes you are right that the error is in below statement
A(ii,jj,kk)=M(r,4);
because when you execute below statement
there might be chances that we will get a vector because there are possibilities that more than 1 index values are same. So, this will give a vector of nx1 dimension. Now, you are trying to insert this vector into a cell of a tensor i.e. A(ii,jj,kk)
Hope this helps :)
SOUMYA SUVRA GHOSAL on 4 Jun 2018
Thank you so much Aakash, can you do me one last favour by telling how to modify the code. Means what should I write in the assignment statement.
Aakash Deep on 5 Jun 2018
Hey Soumya, your approach is correct to populate the tensor. I am not exactly able to figure out why it is behaving like this but here are the few tips you should try:
• After looking at the below statement,