Cost matrix from an array without for loop.
1 view (last 30 days)
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?
the cyclist on 29 Sep 2021
Edited: the cyclist on 29 Sep 2021
You can use implicit expansion to subtract the two vector from each other, by transposing one of them.
cost = abs(domain-domain.');
If you have an older version of MATLAB, that does not support implicit expansion, you can use
cost = abs(bsxfun(@minus,domain,domain'));