Cost matrix from an array without for loop.
Show older comments
Hi I want to create a matrix with entries the absolute value between each point in an array.
My array is called domain, and it has length grid_number.
I have done this in the below code, however I have used a for loop. It takes a long time to run when grid_number is large. Is there a neat way to do this?
for i=1:grid_number
for j=1:grid_number
cost(i,j)=abs(domain(i)-domain(j))
end
end
Accepted Answer
More Answers (1)
cost=abs( domain(:)-domain(:).' );
Categories
Find more on Creating and Concatenating Matrices 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!