How do I substract like variables from each other

1 visualizzazione (ultimi 30 giorni)
Elysia
Elysia il 2 Dic 2022
Risposto: Voss il 3 Dic 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 Commenti

Accedi per commentare.

Risposte (2)

Arif Hoq
Arif Hoq il 2 Dic 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 il 3 Dic 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

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by