Applying custom function to each cell in matrix (or cell array)?

13 ビュー (過去 30 日間)
amanda
amanda 2012 年 3 月 30 日
I have a matrix whose elements are all generated by the random number generator function, rand. ie. A = rand(3,3);
Now I'd quickly and efficiently like to apply a function to each cell in the matrix such that if any element is less than 0.475 that element will be turned into a 1 else it'll be turned into 0.
I believe I'd use cellfun(@func, A) but I'm unsure how to set up the @func correctly since I've always used matrices/vectors and never cell arrays.

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 3 月 30 日
A = rand(3);
out = A < .475
OR use cell array
Ac = num2cell(A);
out = cellfun(@(x)x < .425,Ac,'un',0)

amanda
amanda 2012 年 3 月 30 日
Thank you :)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by