Possible to iterate over table rows without a loop index variable?
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Leon
el 18 de Abr. de 2024
To iterate over the variables (columns) of a table, you can do this:
my_table = table([1; 2; 3], [4; 5; 6], ["Seven"; "Eight"; "Nine"])
for var = my_table
disp(var)
end
Is there a way to adjust this to operate on each row without using an index variable? Using a loop index variable, as follows, works fine but is a bit less elegant than the column-by-column solution.
for row_index = 1:height(my_table)
row = my_table(row_index, :);
disp(row)
end
2 comentarios
Torsten
el 18 de Abr. de 2024
"rows2vars" transposes your table, if it is that what you want to achieve.
Respuesta aceptada
Steven Lord
el 18 de Abr. de 2024
To perform an operation on all rows of a table array you could use rowfun, but that isn't the same as writing a general for loop. I'd personally probably just use the for loop over 1:height(theTableArray).
2 comentarios
Bruno Luong
el 18 de Abr. de 2024
Editada: Bruno Luong
el 18 de Abr. de 2024
Some authority suggests using rowfun on table instead of for-loop row indexing if runtime performance matters.
Read the comments following my complain about combinations only providee table as output format.
Transpose the table as Torsen suggets or retreive the content of the table (without the tanle container) are two other work around of performance hi issue.
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!