convert matrix to tensor
Show older comments
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
Edited: SOUMYA SUVRA GHOSAL
on 4 Jun 2018
Answers (1)
Aakash Deep
on 4 Jun 2018
0 votes
I think this will help you. You can refer this https://www.mathworks.com/matlabcentral/answers/27338-convert-matrix-into-the-tensor.
If this doesn't work you can still ask about it but try to mention more information about the dimensions of input matrix and output tensor.
4 Comments
SOUMYA SUVRA GHOSAL
on 4 Jun 2018
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
[r]=find(M(:,1)==road(ii) & M(:,3)==driver(jj) & M(:,2)==time(kk));
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
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,
[r]=find(M(:,1)==road(ii) & M(:,3)==driver(jj) & M(:,2)==time(kk));
I am assuming that the error is coming from M(:,3)==driver(jj) this part because the value of road can repeat as many time it wants and the value of time can also repeat but what can not repeat is the driver_id for the same pair of road and time (if it does then there is redundancy is your data).
- There can be a possibility that the vector r contains same value multiple times. So just try to use unique on r and then use it as an index value.
- There are highly chances that the vector r can be a null set because of the & operator so you should first check it if it is a single value or not before using it as a index value during assignment. This will fix some of your errors.
Hope this helps :)
Categories
Find more on Time Series Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!