Add new "datetime" elements to an existing cell array (matrix)

3 visualizaciones (últimos 30 días)
Sim
Sim el 24 de Oct. de 2022
Comentada: Star Strider el 24 de Oct. de 2022
What I have so far:
a = cell(3,3);
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2))
idx = 3×1
1 2 9
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
What I need / Desired output: add further elements to the cell array (matrix) "a".
% For example, add the new element "datetime({'00:01:01'})"
% to the cell "index = [3 3]", i.e. "idx = 9":
a =
3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double }
{[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double }
{0×0 double } {0×0 double} {[24-Oct-2022 00:04:21], [24-Oct-2022 00:01:01]}
My attempt:
new_element = datetime({'00:01:01'});
a(9) = a + num2cell(new_element)
Operator '+' is not supported for operands of type 'cell'.

Respuesta aceptada

Star Strider
Star Strider el 24 de Oct. de 2022
I am not certain what a more universal end result would look liks, however this seems to work for this problem —
a = cell(3,3);
index = [1 1
2 1
3 3];
b = datetime({'00:01:35'
'00:01:11'
'00:04:21'});
idx = sub2ind(size(a),index(:,1),index(:,2))
idx = 3×1
1 2 9
a(idx) = num2cell(b)
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21]}
new_element = datetime({'00:01:01'})
new_element = datetime
24-Oct-2022 00:01:01
a{9} = [a{9} new_element] % Concatenate
a = 3×3 cell array
{[24-Oct-2022 00:01:35]} {0×0 double} {0×0 double } {[24-Oct-2022 00:01:11]} {0×0 double} {0×0 double } {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:01]}
The operation is a simple concatenation, using the [] concatenation operators.
.
  6 comentarios
Sim
Sim el 24 de Oct. de 2022
Editada: Sim el 24 de Oct. de 2022
thanks a lot @Star Strider, very very kind!!! :-) Your efforts are very appreciated!
(I have just opened a new question about this issue, but maybe better to close it...)
Star Strider
Star Strider el 24 de Oct. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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!

Translated by