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

조회 수: 5 (최근 30일)
puccapearl
puccapearl 2024년 4월 4일
댓글: Voss 2024년 4월 4일
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!

채택된 답변

Voss
Voss 2024년 4월 4일
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
puccapearl
puccapearl 2024년 4월 4일
Thank you so much!!! I appreciate the help!
Voss
Voss 2024년 4월 4일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by