Subtract first and last value of each cell array in a cell

4 vues (au cours des 30 derniers jours)
puccapearl
puccapearl le 4 Avr 2024
Commenté : Voss le 4 Avr 2024
Hi,
I have a cell made up of several arrays (double). I want to subtract the first and last element of each array into a new cell. Thank you!

Réponse acceptée

Voss
Voss le 4 Avr 2024
F = @(x)x(1)-x(end);
result = cellfun(F,C);
where C is your cell array. Note that since the diffrerence between first and last element of each array is a scalar, you don't need to store the result in a cell array; a regular numeric array will do, and that's what the above code does. However, if you really want the result to be a cell array, just tell cellfun 'UniformOutput',false.
result = cellfun(F,C,'UniformOutput',false);
Adjust the anonymous function as required, e.g., you might actually want @(x)x(end)-x(1) or @(x)abs(x(1)-x(end)).
  2 commentaires
puccapearl
puccapearl le 4 Avr 2024
Thank you so much!!! I appreciate the help!
Voss
Voss le 4 Avr 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by