How do I substract like variables from each other

1 vue (au cours des 30 derniers jours)
Elysia
Elysia le 2 Déc 2022
Réponse apportée : Voss le 3 Déc 2022
I have a table that looks like Book1_1, I need help with figuring out if Var1 row 1 ==Var1 row 2 then subtract Var2 row 2 from Var2 row 1 and continue down till the end of the table (this subtract answer would then create a new variable but I do not need help with creating a new variable just how to subtract likes from likes). Thank you.
  3 commentaires
Elysia
Elysia le 2 Déc 2022

Connectez-vous pour commenter.

Réponses (2)

Arif Hoq
Arif Hoq le 2 Déc 2022
one approach:
a=readmatrix("Book1.xlsx");
b= datetime(a,'ConvertFrom','excel');
% b(:,3)=b(:,2);
for i=1:size(b,1)-1
if b(i,1)==b(i+1,1)
c(i)=b(i+1,2)-b(i,2);
end
end
output=c'
output = 5×1 duration array
00:05:00 00:04:49 00:10:10 00:00:00 00:02:19

Voss
Voss le 3 Déc 2022
a = readmatrix("Book1.xlsx");
b = datetime(a(:,2),'ConvertFrom','excel');
N = size(a,1);
c = duration(NaN(N,3));
for i = 1:N-1
if a(i,1) == a(i+1,1)
c(i+1) = b(i+1)-b(i);
end
end
disp(c);
NaN 00:05:00 00:04:49 00:10:10 NaN 00:02:19

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by