short programs to subtracts rows from a matrix of n length

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Tino
Tino il 22 Apr 2019
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

Accedi per commentare.

Risposta accettata

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

Più risposte (0)

Tag

Non è stata ancora inserito alcun tag.

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by