function for cell array

2 ビュー (過去 30 日間)
Oskar Kilgus
Oskar Kilgus 2022 年 7 月 28 日
編集済み: Jon 2022 年 8 月 2 日
Hi folks,
i´ve got a cell array with vectors of data type double in every cell. Now i want to apply a function to every single cell. I found the command cellfun(func,A) to be useful.
Nonetheless i dont know how to implement a function func to do what i want on every part of the cell array. In particular i dont get how to tell the function that the vectors of doubles in the cells of A are the input arguments.
I´m thankful for any advice!

採用された回答

Jon
Jon 2022 年 7 月 28 日
Here's a simple example
% Make an example cell array with a vector in each cell
A = cell(2,3); % preallocate
for i = 1:2
for j = 1:3
A{i,j} = rand(4,1);
end
end
A
A = 2×3 cell array
{4×1 double} {4×1 double} {4×1 double} {4×1 double} {4×1 double} {4×1 double}
% Now use cellfun to compute the sum of the elements in each vector
B = cellfun(@(x) sum(x),A)
B = 2×3
1.8600 1.9744 2.3527 2.2433 2.3225 2.4160
  3 件のコメント
Oskar Kilgus
Oskar Kilgus 2022 年 8 月 2 日
Thanks for the initial help Jon,
i guess the problem i´m dealing with is more about the function itself.
I´ve got a cell array with wave-data in every cell and want to apply the pitch function to it.
As mentioned above im struggling with the input for pitch. Lets take ur example:
% Make an example cell array with a vector in each cell
A = cell(2,3); % preallocate
for i = 1:2
for j = 1:3
A{i,j} = rand(4,1);
end
end
A
A = 2×3 cell array
{4×1 double} {4×1 double} {4×1 double} {4×1 double} {4×1 double} {4×1 double}
The idea is now to do the following:
f0_A = cellfun(pitch(???),A)
I cant figure out how to tell the pitch function what the right input arguments are..
Thanks in advance!
Jon
Jon 2022 年 8 月 2 日
編集済み: Jon 2022 年 8 月 2 日
I am not familiar with the "pitch" function, and it does not seem to be in my MATLAB R2022A, or any of my toolboxes. Maybe you have the Audio Toolbox? I see looking online that there is a pitch function in that toolbox https://www.mathworks.com/help/audio/ref/pitch.html#mw_cb9b23c6-9dfa-4f98-8596-743e701e5fb8. Assuming this is the function you are using and you have already defined the variable fs in your workspace, you could do something like:
f0_A = cellfun(@(x) pitch(x,fs),A)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by