the dimensions of matrix and the size is different?
4 views (last 30 days)
Show older comments
I reshaped the dimensions of my matrix in my code. When i run this:
x=[M,N] y=[M2,P]
i get the following answer x =
335 80
y =
335 2
However, when i check the size with this command sz1= size(x) sz2=size(y)
i get the following answer: sz1 =
1 2
sz2 =
1 2
my code is this
function d = disteu(x, y) % DISTEU Pairwise Euclidean distances between columns of two matrices % % Input: % x, y: Two matrices whose each column is an a vector data. % % Output: % d: Element d(i,j) will be the Euclidean distance between two % column vectors X(:,i) and Y(:,j) % % Note: % The Euclidean distance D between two vectors X and Y is: % D = sum((x-y).^2).^0.5
% D = sum((x-y).^2).^0.5
[M, N] = size(x);
[M2, P] = size(y);
if (M ~= M2)
C=padarray(x,[21,0],0,'post');
sz=size(C);
[M,N]= size(C);
end x=[M,N] y=[M2,P]
sz1= size(x) sz2= size(y)
d = zeros(N, P);
if (N < P) copies = zeros(1,P); for n = 1:N d(n,:) = sum((x(:, n+copies) - y) .^2, 1); end else copies = zeros(1,N); for p = 1:P d(:,p) = sum((x - y(:, p+copies)) .^2, 1)'; end end
d = d.^0.5;
2 Comments
M
on 21 Mar 2018
If
x =
335 80
then it is a vector of dimension 1x2 : one row, two columns. So the size that you get does correspond. What is the problem ?
Jan
on 21 Mar 2018
Please read http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099 for formatting code in the forum.
SQRT is faster than the power operation .^0.5
Answers (1)
Jan
on 21 Mar 2018
This is exactly the expected behavior.
x = [M,N]
y = [M2,P]
This concatenates the scalars M and N, as well as M2 and P horizontally. Then both variables have the dimensions [1, 2], as the size() command tells you. The contents of x and y can be [335, 80] and [335, 2], but this is no contradiction.
See Also
Categories
Find more on Logical 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!