Create a simple table from using data from other table and an Iteration loop
1 次查看(过去 30 天)
显示 更早的评论
Dear all,
I am very frustrated trying to create a very basic thing in Matlab for the past 4 hours with no success.
i have a table named time = [0;1;2;3;4;5;6;7;8;9;10;12;14;16;18;20;22;25;28;30];
I am trying to make a second table dt that will be the difference of the current minus the previous time.
dt = [1;1;1;1;1;1;1;1;1;1;1;2;2;2;2;2;2;3;2] using the following loop:
for i=2:20
k=time(i)-time(i-1);
dt(i-1)= k;
end
but I get the following error:
Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row
subscript and a variable subscript.
Please help!!!
BR
1 个评论
Stephen23
2019-4-29
>> time = [0;1;2;3;4;5;6;7;8;9;10;12;14;16;18;20;22;25;28;30];
>> diff(time)
ans =
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
3
3
2
采纳的回答
Jeff Miller
2019-4-29
If you are really using the table datatype (as the error message suggests), then you are not indexing it properly. A table has variables, and you can index those as you are trying to do. So, I imagine you would need something like
for i=2:20
k=time.time(i)-time.time(i-1);
dt(i-1)= k;
end
It is a bit confusing to use 'time' as the name of both a table and a variable within that table, though you can do that if you want.
Looking at your code, though, I wonder why you need to use the table datatype at all. Why not just have a vector for time and a vector for dt?
3 个评论
Jeff Miller
2019-4-29
Pretty much like you already are doing it, but without the table datatype. For example,
clear all
time = [0;1;2;3;4;5;6;7;8;9;10;12;14;16;18;20;22;25;28;30];
dt = zeros(numel(time)-1,1);
for i=2:20
k=time(i)-time(i-1);
dt(i-1)= k;
end
Actually there is also a nice matlab function called 'diff' for successive differences, so you could just use that:
time = [0;1;2;3;4;5;6;7;8;9;10;12;14;16;18;20;22;25;28;30];
dt = diff(time);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!