using conditional statements inside cellfun
75 ビュー (過去 30 日間)
古いコメントを表示
Kaushik Lakshminarasimhan
2017 年 11 月 12 日
コメント済み: Kaushik Lakshminarasimhan
2017 年 11 月 12 日
Suppose I have two cell arrays X and Y. If Y{i} satisfies a condition, I want to transform X{i} into some X'{i}, otherwise leave X{i} unchanged. Something like:
for i=1:N
if Y{i}>2, X_out{i} = reshape(X_in{i},...);
else X_out{i} = X_in{i}; end
end
How can I do this with cellfun? Multiplying by false won't work because the transformation involves reshaping X{i}. For example, the following doesn't work:
X_out = cellfun(@(x,y) ~(y>2)*x + (y<=2)*reshape(x,...), X_in, Y);
I presume this question applies to anonymous functions in general.
0 件のコメント
採用された回答
Walter Roberson
2017 年 11 月 12 日
You cannot use those kinds of conditionals in cellfun, at least not without the use of an auxillary true function that does the equivalent of the C/C++ question-colon operator.
The workaround is
mask = cellfun(@(y) y>2, Y);
X_out = X_in;
X_out(mask) = cellfun(@(xin) reshape(xin, ...), X_in(mask), 'uniform', 0);
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!