Avoid a "loop for" to add multiple "datetime" elements to a cell array

Given the matrix "a"
% (1) create the cell array / matrix "a"
a = cell(3,3);
% (2) add the first elements to the cell array / matrix "a"
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));
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]}
I would like not to use the loop for, here below, to add multiple elements to different cells:
% (3) add two new elements:
% - one new element inside the cell [2 3]
% - one new element inside the cell [3 3]
index2 = [2 3
3 3];
new_elements = datetime({'00:01:01'
'00:01:56'});
idx2 = sub2ind(size(a),index2(:,1),index2(:,2));
for i = 1 : size(idx2,1)
a{idx2(i)} = [a{idx2(i)} new_elements(i)];
end
% Desired output, but without the loop for
a,
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} {[24-Oct-2022 00:01:01 ]} {0×0 double } {0×0 double} {[24-Oct-2022 00:04:21 24-Oct-2022 00:01:56]}

5 个评论

Why are you making such a funky data storage pattern in the first place? That's going to be terrible to try to use for anything mixing up types/sizes (especially the types/classes) because arguments to cellfun functions are going to barf when they hit the different classes the functions aren't able to handle.
thanks for the comment @dpb... if there are other methods to make the same job, I will follow them with pleasure :-)
Would have to know the nature of the "job" -- what is the end result and why is the above organization what you came up with so far?
Data design is key in creating a functionality, but the end specifications for the result drive the whole process.
We have none of that to go on other than what looks to be an ugly mess to try to deal with as is...
thanks for your comment @dpb .... Do you have an alternative suggestion to achieve the same goal ?
We don't know what "the goal" is other than the ugly-looking mess, so how can we have any suggestions about achieving same?
Is it really that your starting cell array should have been
>> N=3;
>> A=num2cell(NaT(N))
A =
3×3 cell array
{[NaT]} {[NaT]} {[NaT]}
{[NaT]} {[NaT]} {[NaT]}
{[NaT]} {[NaT]} {[NaT]}
>>
???
In that case, then at least all the elements of the cell array are datetime and you won't have the issue I raised above about having to deal with the problems of having to operate on different classes by location in the array.
But, even there I don't see a syntax without some form of a loop to do the catenation of additional elements.

请先登录,再进行评论。

回答(1 个)

If you can make is an N by 1 cell array and do the assignment using parenthesis indexing and num2cell, then convert that to the J,K array size where J*K == N, then maybe it works for you?
b = cell(9,1);
b =
9×1 cell array
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
{0×0 double}
>> b(idx2) = num2cell(new_elements)
b =
9×1 cell array
{0×0 double }
{0×0 double }
{0×0 double }
{0×0 double }
{0×0 double }
{0×0 double }
{0×0 double }
{[24-Oct-2022 00:01:01]}
{[24-Oct-2022 00:01:56]}

1 个评论

Thanks for your answer @Benjamin Thompson, but it does not look like the same "desired output", as I indicated in my question...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Type Identification 的更多信息

提问:

Sim
2022-10-24

编辑:

dpb
2022-10-26

Community Treasure Hunt

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

Start Hunting!

Translated by