How to perform arithmetic operation in pdist?

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

Please provide us the complete program.

The complete code is too long, here i simplify the value of code.

numNodes  = 100
indPCHTier1 =  1     3     5     7    13    18    21
squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] )) = have Columns 1 through 25 with random number. 

eg:

Then, how can i put the value of "squareform (pdist)" into distAllTier1(i,j) ?

Thanks.

A1  =  pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] );
distAllTier1(i,j)  = squareform(A1);

I try to make something different. Still error.

Here is previous code:

distAllTier1(i,j) = sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2); 

Sign in to comment.

Answers (2)

            distAllTier1{i,j}   = squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ));

4 Comments

Sir, it came out the cell error.

Cell contents assignment to a non-cell array object.

Error in mapmulti (line 236)
            distAllTier1{i,j}   = squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ));
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.

Actually, the distAllTier1 came from this line:

distAllTier1 = zeros(numNodes,numPCHTier1);

which numNodes=100 and numPCHTier1=7. Then distAllTier1 result will be 0 in 100 lines as follow:

>> length(distAllTier1)
ans =
     100

I just want to make the,

squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ))

result, save in distAllTier1(i,j). How can i do it? Any advice?

distAllTier1 = cell(numNodes,numPCHTier1);

Sign in to comment.

Why not just use pdist2() and not worry about squareform()?
I always use pdist2() and never use pdist().

6 Comments

like this?

pdist2([node(j).x - node(i).x] + [node(i).y - node(j).y] );
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.)
It has an error sir,
Subscript indices must either be real positive integers or logicals.
Error in xcuba1 (line 140)
node(i).dist = pdist2( [node(i).x(:), node(i).y(:)], [node(j).x(:), node(j).y(:)] )
Then i try this,
node(i).dist = squareform( pdist([x, y]) );
I suspect that you have not defined i or j at that point. Difficult to say without the code to examine.
This is a original code that use single input:
distAllTier1(i,j) = sqrt((node(i).x - baseStation.x).^2 + (node(i).y - baseStation.y).^2);
I and J use to find the distance between them. I modify the code to make it able to operate in multiple vector. Then is use this,
distAllTier1(i,j) = squareform (pdist([node(j).x - node(i).x] + [node(i).y - node(j).y] ));
I dont know either in multiple vector it need to find distance between i & j or not.
Make it easy to help you. Attach your node variable in a .mat file.

Sign in to comment.

Categories

Asked:

on 30 Sep 2018

Commented:

on 2 Oct 2018

Community Treasure Hunt

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

Start Hunting!