short programs to subtracts rows from a matrix of n length

1 次查看(过去 30 天)
Hi,
Please kindly help me with a short program that can do this computation using T.length and X.length. Please find my computation in matlab below and the instruction on every step.
Thanks in advance
Jonathan
The final computation answer is also given
T= [5.1000 3.5000 1.4000 0.2000;
6.4000 3.2000 4.5000 1.5000;
5.4000 3.4000 1.7000 0.2000;
5.7000 2.8000 4.5000 1.3000;
5.7000 4.4000 1.5000 0.4000;
5.6000 2.9000 3.6000 1.3000];
X = [
5.1000 3.8000 1.9000 0.4000;
6.4000 2.9000 4.3000 1.3000;
4.3000 3.0000 1.1000 0.1000;
5.4000 3.0000 4.5000 1.5000];
Z1 = T - X (1, :);
Z2 = T - X(2,:);
Z3 = T - X(3,:);
Z4 = T - X (4,:);
% get the square of each rows%
Z1 = (Z1.^ 2);
Z2 = (Z2.^2);
Z3 = (Z3.^2);
Z = (Z4.^2);
% get the sum of each matrices %
Z1 = sum(Z1, 2);
Z2 = sum(Z2, 2);
Z3 = sum(Z3, 2);
Z4 = sum(Z4, 2);
% get the square root of the numbers in each matrices to get the
% distance%
Z1 = sqrt(Z1);
Z2 = sqrt(Z2);
Z3 = sqrt(Z3);
Z4 = sqrt(Z4);
Answers from the computation below
Z1 =
0.6164
3.1654
0.5745
2.9883
0.9381
2.1817
Z2=
3.4161
0.4123
3.0364
0.7348
3.3749
1.0630
Z3=
0.9950
4.2391
1.3191
3.8730
2.0421
3.0643
Z4 =
3.4117
1.0198
3.1129
0.4123
3.5014
0.9487
  2 个评论
Tino
Tino 2019-4-22
Hi Madhan thanks for your response.
I just want a shorter code to do the computation above using any 2 matrices with varying length
T = T.length
X = X.length
Please get back to me if you need clarification
thanks in advance

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-4-22
Z = squeeze(sqrt(sum((T - permute(X,[3,2,1])).^2,2)));

更多回答(0 个)

标签

尚未输入任何标签。

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by