How to perform arithmetic operation in pdist?
Show older comments
Hi, i need some advice for solving the error of using squareform,
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in mapmulti (line 234)
distAllTier1(i,j) = squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ));
And the code as follow:
for i = 1:1:numNodes
for j = indPCHTier1 %indPCHTier1 = 1 3 5 7 13 18 21 = j(7)
if node(i).tier == 1
if i == j
distAllTier1(i,j) = NaN;
else
distAllTier1(i,j) = squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ));
end
end
end
end
Thanks :D
3 Comments
ANKUR KUMAR
on 30 Sep 2018
Please provide us the complete program.
Asyran Abdullah
on 30 Sep 2018
Asyran Abdullah
on 30 Sep 2018
Answers (2)
Walter Roberson
on 1 Oct 2018
1 vote
distAllTier1{i,j} = squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ));4 Comments
Asyran Abdullah
on 1 Oct 2018
Walter Roberson
on 1 Oct 2018
You have a left over distAllTier1 in your workspace that you need to clear. Or else you have an initialization of distAllTier1 that you did not show.
Asyran Abdullah
on 1 Oct 2018
Walter Roberson
on 1 Oct 2018
distAllTier1 = cell(numNodes,numPCHTier1);
Image Analyst
on 1 Oct 2018
1 vote
Why not just use pdist2() and not worry about squareform()?
I always use pdist2() and never use pdist().
6 Comments
Asyran Abdullah
on 1 Oct 2018
Walter Roberson
on 1 Oct 2018
pdist2( [node(i).x(:), node(i).y(:)], [node(j).x(:), node(j).y(:)] )
perhaps.
This assumes that node(i).x(K) and node(i).y(K) together name X, Y coordinates, so node(i).x and node(i).y are a list of corresponding x and y coordinates for a list of distinct points associated with node(i), and that node(j) likewise has a list of distinct points associated with it, and that you want to find the distance from each point in node(i) to each point in node(j)
The result would be a symmetric 2D matrix of distances.
If your situation is instead that node(i).x and node(i).y both form part of multidimensional coordinates for a single multidimensional point node(i) and likewise there is a single multidimensional point designated for node(j) and you want to find the multidimensional distance, then you would need different code (and you would need to explain why you bothered to split multidimensional coordinates into two components.)
Asyran Abdullah
on 2 Oct 2018
Walter Roberson
on 2 Oct 2018
I suspect that you have not defined i or j at that point. Difficult to say without the code to examine.
Asyran Abdullah
on 2 Oct 2018
Edited: Asyran Abdullah
on 2 Oct 2018
Image Analyst
on 2 Oct 2018
Make it easy to help you. Attach your node variable in a .mat file.
Categories
Find more on Interpolation 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!