Filter löschen
Filter löschen

How to add zeroes to a matrix to make the dimensions same.

13 Ansichten (letzte 30 Tage)
Bisma Waheed
Bisma Waheed am 21 Mär. 2018
Bearbeitet: Stephen23 am 21 Mär. 2018
I am not able to convey my problem. My code is really long hence i am not able to upload it. could you tell me this my matrix dimensions are A= 335 by 16 B=314 by 1 How can i add zeroes to make B= 335 by 16
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)
error('Matrix dimensions do not match.') end
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;
I want to remove the error "matrix dimensions do not match"
  2 Kommentare
Jan
Jan am 21 Mär. 2018
Bearbeitet: Jan am 21 Mär. 2018
@Bisma Waheed: Please do not ignore the already given advice to format your code, see Tutorial: Format code . It is in your interest, that your code can be read without difficulties. The process is trivial: Mark the code with the mouse and press the "{} Code" button. Thank you.
You mention "A" and "B" in the question, but it is not clear, which variables in the code you mean. I do not think that filling a matrix with zeros helps to calculate the Pairwise Euclidean Distances.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 21 Mär. 2018
Bearbeitet: Stephen23 am 21 Mär. 2018
B(335,16) = 0;
will automatically expand the matrix B to have size 335x16, the added elements have the default value (zero). Note that you will need to add code to check if B already has enough rows.
The question contains the code comment "Pairwise Euclidean distances between columns of two matrices", but it is not clear how inventing data helps this calculation.

Community Treasure Hunt

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

Start Hunting!

Translated by