필터 지우기
필터 지우기

convert matrix to tensor

조회 수: 7 (최근 30일)
SOUMYA SUVRA GHOSAL
SOUMYA SUVRA GHOSAL 2018년 6월 4일
댓글: Aakash Deep 2018년 6월 5일
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
SOUMYA SUVRA GHOSAL
SOUMYA SUVRA GHOSAL 2018년 6월 4일
편집: SOUMYA SUVRA GHOSAL 2018년 6월 4일
here's my code,please tell me where is the error
if
road=unique(M(:,1));
time_slot=unique(M(:,2));
user_id=unique(M(:,3));
n_road=numel(road);
n_time=numel(time_slot);
n_user=numel(user_id);
driver=intersect(user_id,randsample(user_id,100));
A=ones(n_road,100,n_time);
for ii=1:n_road
for jj=1:100
for kk=1:n_time
[r]=find(M(:,1)==road(ii) & M(:,3)==driver(jj) & M(:,2)==time(kk));
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);

댓글을 달려면 로그인하십시오.

답변 (1개)

Aakash Deep
Aakash Deep 2018년 6월 4일
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
SOUMYA SUVRA GHOSAL
SOUMYA SUVRA GHOSAL 2018년 6월 4일
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
Aakash Deep 2018년 6월 5일
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 :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by