Hello All!. I need to create a matrix 81*81. I've done some coding below but it gives error (i.e. I need to get Base_Matrix, but at the end I get all the values zero). I've two matrices files i.e. A (61312*3) and B (246*1)
Total_Surveyed_Trucks=size(A,1);
Total_Survey_Number=max(B);
Base_Matrix=zeros(81,81);
for i=1:Total_Survey_Number
Survey_Matrix=zeros(81,81);
for j=1:Total_Surveyed_Trucks
Origin=Survey(j,2);
Destination=Survey(j,3);
if Survey(j,1)==i
if Origin~=Destination
Survey_Matrix(Origin,Destination);
end
end
end
Base_Matrix=Base_Matrix+Survey_Matrix;
end
Can anyone help me out? Thanks in advance.

 Accepted Answer

I can’t run your code or simulate your data (I don’t know what it looks like), but this line:
Survey_Matrix(Origin,Destination);
is likely your problem. All it does is bring that value into the workspace. It doesn’t assign anything to it, or it to anything else.

7 Comments

Muhammad’s ‘answer’ moved here. It is a comment and not an answer.
------------------------------------
Thanks for your reply.
What if I do like this instead of that line:
Survey_Matrix(Origin,Destination)=Survey_Matrix(Origin,Destination)+SurveyLocation(:)
Muhammad's reply moved here:
Thanks for your reply.
What if I do like this instead of that line:
Survey_Matrix(Origin,Destination)=Survey_Matrix(Origin,Destination)+SurveyLocation(:)
It's easy enough to try it and see that it won't work because SurveyLocation is not defined, and even if it were it couldn't be an array since you're sticking it into an element meant for a single number.
Better, but still not a solution. The reason is that:
SurveyLocation(:)
creates a column vector out of all the elements of SurveyLocation. This will result in an error, because MATLAB cannot assign the entire vector to one scalar, that being Survey_Matrix(Origin,Destination). You have to choose one element of SurveyLocation, not the entire vector.
Fayyaz
Fayyaz on 21 Jun 2014
Edited: Star Strider on 21 Jun 2014
I've done the below procedure but still get zero values in my base_matrix.
%%%%%%%%%%%%%%%%%%%%%%SurveyLocation%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%SurveyLocation(:,1)= Unique Survey Id
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%Survey%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Survey(:,1)= Unique Survey Id
%Survey(:,2)= Origin Province of the Truck
%Survey(:,3)= Destination Province of the Truck
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Total_Surveyed_Trucks=size(Survey,1);
Total_Survey_Number=max(SurveyLocation(:,1));
Base_Matrix=zeros(81,81);
for i=1:Total_Survey_Number
Survey_Matrix=zeros(81,81);
for j=1:Total_Surveyed_Trucks
Origin=Survey(j,2);
Destination=Survey(j,3);
if Survey(j,1)==i
if Origin~=Destination
Survey_Matrix(Origin,Destination)=Survey_Matrix(Origin,Destination)+SurveyLocation(i,1);
end
end
end
Base_Matrix=Base_Matrix+Survey_Matrix;
end
I suspect that the condition:
Survey(j,i) == i
is never true, so your SurveyMatrix never gets updated.
Attach your m-file and data file(s) with the paper clip icon if you want more advice. Don't make us guess and waste our time. Make it easy for us to help you, not hard.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!