Kindly explain this command

final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
Kindly explain this command. Basically i want to add alto and treb and they are being added. what does uniform and 0 mean in this command

 採用された回答

Grzegorz Knor
Grzegorz Knor 2011 年 9 月 22 日

1 投票

Option Uniform or UniformOutput set to 0 (or false) means, that output from function will be stored in cell array. It is necessary when function (in your case plus) returns non scalar values. If you set the UniformOutput value to 1 ( or true), your function must return scalar values and the output will be concatenated into an array.
Look at examples:
a = {rand(1),rand(2),rand(3),rand(4)}
b = {rand(1),rand(2),rand(3),rand(4)}
cellfun(@plus,a,b,'UniformOutput', false)
a = {rand(1),rand(1),rand(1),rand(1)}
b = {rand(1),rand(1),rand(1),rand(1)}
cellfun(@plus,a,b,'UniformOutput', false)
a = {rand(1),rand(1),rand(1),rand(1)}
b = {rand(1),rand(1),rand(1),rand(1)}
cellfun(@plus,a,b,'UniformOutput', true)

5 件のコメント

moonman
moonman 2011 年 9 月 22 日
why my function will return non scaler value.
i think it will return scaler values as they are just numerical values
kindly explain term non scaler
Grzegorz Knor
Grzegorz Knor 2011 年 9 月 22 日
I don't know why, perhaps your variables alto and treb contain matrices.
moonman
moonman 2011 年 9 月 22 日
alto and trebs are ARRAYS having 300 cells
and each cell is having a row vector of 700 elements
or 700 columns and one row
Grzegorz Knor
Grzegorz Knor 2011 年 9 月 22 日
So you have the answer. The sum of two matrices (vectors) is a matrix (vector) and not a scalar.
moonman
moonman 2011 年 9 月 22 日
Thanks a lot

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

その他の回答 (1 件)

Jan
Jan 2011 年 9 月 22 日

1 投票

Have you read "help cellfun" and "<http://www.mathworks.com/help/techdoc/ref/cellfun.html doc cellfun>" already?
There you find the information, that 'Uniform' is an abbreviation for 'UniformOutput' - sometimes neato users write 'Uni' only. And the description is:
a logical value indicating whether or not the output(s) of FUN can be
returned without encapsulation in a cell array
Actually the parameter should be of type LOGICAL, but MATLAB converts the 0 kindly to FALSE, as a 1 is converted to TRUE. Try this:
f = cellfun(@plus, {1}, {2}, 'Uniform', 0);
disp(f);
disp(class(f));
f = cellfun(@plus, {1}, {2}, 'Uniform', 1);
disp(f);
disp(class(f));
f = cellfun(@plus, {1}, {[2, 3]}, 'Uniform', 0);
disp(f);
disp(class(f));
% But this fails, because the output cannot be written to a vector,
% because the elements have different sizes:
f = cellfun(@plus, {1}, {[2, 3]}, 'Uniform', 1);

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by