I have a problem using MATLAB. I want to create a huge adjacency matrix and to achieve this result I wrote some code that generates 3 vectors: NodeX
, NodeY
, and Weight
.
NodeX
represents the row indexes of the matrix.
NodeY
represents the column indexes of the matrix.
Weight
is a vector that contains the weights.
All these vectors are put into a .csv
file that MATLAB reads very easily. But when I create the matrix I have a problem. This is the code that I use to associate indexes to weights.
for i=1:1:3873
WeightedMatrix(NodeY(i), NodeX(i)) = Weight(i);
end
The results I see are a little bit strange to me. In fact if I do:
WeightedMatrix(NodeY(1), NodeX(1))
MATLAB prints to me:
ans = 0.2483
Which is a value that is present into the Weight vector but it's not the value I want.
And if I do:
Weight(1)
It returns ans = 1.2550
(which is the value I want).
These three vectors are for example:
This is the final part that creates the .csv
file:
tmp = set(Final)
tmp = tuple(tmp)
for z in tmp:
print str(z[0]) + "," + str(z[1]) + "," + str(z[2]) + "\n"
You can form adjacency matrix as a sparse matrix to prevent memory problem:
WeightedMatrix_sp = sparse(NodeX, NodeY, Weight);
If you have enough memory you can convert sparse matrix to dense matrix:
WeightedMatrix = full(WeightedMatrix_sp);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.