Add support for applying a function to every variable within a CodimensionalCollection #3

Open
opened 2025-11-19 02:08:41 +00:00 by UserCurt · 0 comments
UserCurt commented 2025-11-19 02:08:41 +00:00 (Migrated from codeberg.org)

It would be quite convenient to apply a function to all variables within a CodimensionalCollection at the same time, returning a new CodimensionalCollection as a result. For example, one could perform mean(C) which would apply the mean function to each entry of the CodimensionalCollection C. Of course, not all methods can be known in advance, especially when C contains custom classes with custom methods, so it may be better to define an apply method:

C = CodimensionalCollection(toeplitz(1:3), hankel(1:3), [1; 2; 3], [4, 5, 6]);

% Apply mean to each variable
R = C.apply(@mean)
R = CodimensionalCollection(mean(C.Var1), mean(C.Var2), mean(C.Var3), mean(C.Var4)); % Equivalent

% Apply mean(__, 2) to each variable
R = C.apply(@mean, 2)
R = CodimensionalCollection(mean(C.Var1, 2), mean(C.Var2, 2), mean(C.Var3, 2), mean(C.Var4, 2)); % Equivalent

The name apply is not settled. Alternatives to consider include map, transform, foreach. Though foreach doesn't suggest it returns a value so is probably not a good choice.

It may be appropriate to also apply to only a subset of variables. The apply method cannot be overloaded to account for this as it would be ambiguous whether any additional arguments should be passed in or not. Instead, a new function like applySubset could be made that that applies a mapping to a subset of variables. However, it would be unclear whether it keeps the others intact or omits them. It may be more appropriate to instead of a select method that selects a subset of variables and then subsequently using apply. This is out of scope for this feature request, but it could influence whether a method like applySubset is developed or not.

It would be quite convenient to apply a function to all variables within a CodimensionalCollection at the same time, returning a new CodimensionalCollection as a result. For example, one could perform `mean(C)` which would apply the `mean` function to each entry of the CodimensionalCollection `C`. Of course, not all methods can be known in advance, especially when `C` contains custom classes with custom methods, so it may be better to define an `apply` method: ```matlab C = CodimensionalCollection(toeplitz(1:3), hankel(1:3), [1; 2; 3], [4, 5, 6]); % Apply mean to each variable R = C.apply(@mean) R = CodimensionalCollection(mean(C.Var1), mean(C.Var2), mean(C.Var3), mean(C.Var4)); % Equivalent % Apply mean(__, 2) to each variable R = C.apply(@mean, 2) R = CodimensionalCollection(mean(C.Var1, 2), mean(C.Var2, 2), mean(C.Var3, 2), mean(C.Var4, 2)); % Equivalent ``` The name `apply` is not settled. Alternatives to consider include `map`, `transform`, `foreach`. Though `foreach` doesn't suggest it returns a value so is probably not a good choice. It may be appropriate to also apply to only a subset of variables. The `apply` method cannot be overloaded to account for this as it would be ambiguous whether any additional arguments should be passed in or not. Instead, a new function like `applySubset` could be made that that applies a mapping to a subset of variables. However, it would be unclear whether it keeps the others intact or omits them. It may be more appropriate to instead of a `select` method that selects a subset of variables and then subsequently using `apply`. This is out of scope for this feature request, but it could influence whether a method like `applySubset` is developed or not.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
UserCurt/collections-toolbox#3
No description provided.