MATLAB Answers

Function calculating distance between pixels in 1x1x3 arrays

7 views (last 30 days)
I have a function which calculates square distance between pixels, however it wants to calculate the distance between 2 pixels which has a 1x1x3 array.
For example, inputs P and Q both have 1 row, 1 column and 3 colour layers, and single output is squared distance.
I found this function on matlab forums which calculates squared distance and it gives correct results:
function [SquaredDistance] = PixelDistance(P,Q)
P1 = P(1); P2 = P(2); P3 = P(3);
Q1 = Q(1); Q2 = Q(2); Q3 = Q(3);
SquaredDistance = (P1-Q1)^2 + (P2-Q2)^2 + (P3-Q3)^2;
However, if I have a table (as an example) like this: (where :,:,1 is red values :,:,2 is green values etc)
Colours(:,:,1) =
Colours(:,:,2) =
Colours(:,:,3) =
How do we calculate squared value using input commands such as P = Colours(1,1,:) and Q = Colours(3,1,:) using the code?


Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 7 Sep 2019
The function you found is rather badly coded ...
Another expression for "calculating a distance" is "taking the norm". Matlab has a function for that, called NORM :-) When you have two points in 3D (colour, RGB) space, P and Q, represented as vectors with three values, you can calculated the distance by looking at the vector between P and Q:
P = [10 ; 100 ; 10], Q = [30 ; 40 ; 50] % example
norm(P-Q) % which is the same as
In your examples you have a your 3D points (vectors) stacked up in a single array Colours. It looks like Colours is a 3-by-1-by-3 array. You can get rid of the obfusciating second dimension using squeeze
Colours = randi(100,[3 1 3]) % example of your data ??
C = squeeze(Colours) % a 3-by-3 array: each column is a point


Show 1 older comment
Jos (10584)
Jos (10584) on 7 Sep 2019
You're welcome.
RANDI is a function that returns an array of a specific size filled with random numbers. The first argument is the upper limit, the other arguments are size argument.
You can easily find out what a function does using matlab's excellent help and documentation system. That is how still, after many many years, learn about (new) functions and how to use them. It is also shows that you should always write a decent help section when writing your own function to make it useful for others :-)
>> help randi
Sara Foster
Sara Foster on 7 Sep 2019
Oh okay, there also seems to be an error when I type P = Colours(1,1,:) as it shows up "Undefined function"....
Jos (10584)
Jos (10584) on 7 Sep 2019
In this code, Matlab expects Colours to be a variable in your workspace. I think you would benefit greatly from looking at an introduction course, like Matlab Onramp

Sign in to comment.

More Answers (1)

Sara Foster
Sara Foster on 7 Sep 2019
Okay thank you again, and another problem I'm finding is (C(1,:)-C(2,:)) only subtracts row 2 from row 1, how do we make it so that our input commands determine which row it needs to be subtracted from e.g. Q = Colours(3,1,:) and P = Colours(1,1,:), we are required to subtract row 3 from row 2 using P,Q command.

  1 Comment

Jos (10584)
Jos (10584) on 8 Sep 2019
I do not get this question, sorry. Also, do not pose a new question as an answer here but open a new thread, or put it as a comment/follow-up on a given answer, to avoid confusion :-)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!