How do I substract like variables from each other

1 次查看(过去 30 天)
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.

回答(2 个)

Arif Hoq
Arif Hoq 2022-12-2
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 2022-12-3
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

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by