Create a simple table from using data from other table and an Iteration loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Michael Perakis
el 28 de Abr. de 2019
Comentada: Stephen23
el 29 de Abr. de 2019
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 comentario
Stephen23
el 29 de Abr. de 2019
>> 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
Respuesta aceptada
Jeff Miller
el 29 de Abr. de 2019
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 comentarios
Jeff Miller
el 29 de Abr. de 2019
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);
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!