the dimensions of matrix and the size is different?

4 ビュー (過去 30 日間)
Bisma Waheed
Bisma Waheed 2018 年 3 月 21 日
コメント済み: Jan 2018 年 3 月 21 日
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 件のコメント
M
M 2018 年 3 月 21 日
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
Jan 2018 年 3 月 21 日
SQRT is faster than the power operation .^0.5

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2018 年 3 月 21 日
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.
  6 件のコメント
Jan
Jan 2018 年 3 月 21 日
@Bisma:
B = rand(314, 1); % For testing only, use your data
B(355, 16) = 0
fills all needed elements with zeros automatically.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by