cellfun error when using

2 次查看(过去 30 天)
Hi everyone, can you help me for my code? this is my code:
clear all
samples = [ 88 93 82 39 58 55 90 68 4 78 85 95 24 57 8 68
66 88 45 16 24 67 32 66 97 78 85 58 73 16 37 18
6 32 59 44 81 22 31 94 24 61 68 91 88 62 79 66
100 93 13 83 39 37 29 28 22 80 59 79 22 85 92 97
8 50 21 6 14 84 93 34 20 4 51 60 100 72 55 13
39 10 8 49 13 52 69 47 40 73 42 45 83 9 58 49
89 20 82 84 37 74 68 56 54 93 99 36 44 90 26 50
37 15 73 73 11 37 33 19 97 25 21 75 56 85 68 83
98 12 62 61 59 78 39 9 78 21 53 25 74 21 73 20
52 84 87 58 33 5 38 99 88 21 16 15 73 42 6 23
67 3 55 10 20 44 30 11 53 85 41 46 23 71 46 79
72 79 16 38 86 88 19 69 43 64 21 30 74 57 8 32
100 75 64 61 86 7 93 91 43 44 34 49 81 30 95 55
53 14 84 89 62 8 24 63 100 48 63 54 80 16 81 88
8 61 73 96 54 5 83 96 59 90 72 75 98 40 71 76
100 6 17 21 32 65 6 68 99 29 79 18 20 70 49 85];
threshold = 90;
for r=1:16
for c=1:16
CellActivate(r,c) = {samples(r,c)};
CellConfig.Boosts{r,c} = rand(2,2);
CellConfig.value{r,c} = 2*ones(2,2);
end
end
E = cellfun( @(B,C) B(C>threshold)+ones(2,2) , CellConfig.Boosts, CellActivate,'UniformOutput',false);
my point is E will return value of B( when C > threshold) plus ones(2,2).
Thank for your help.

采纳的回答

Walter Roberson
Walter Roberson 2018-7-19
Each of your CellActivate entries is a scalar. Your threshold is a scalar. For any one entry in CellActivate, comparing it to a scalar is going to get you a scalar true or false.
Your CellConfig.Boosts entries are 2 x 2. Indexing a 2 x 2 at a scalar logical is going to get you either emptiness or a scalar double.
This value that is either emptiness or a scalar double is added to a 2 x 2 array. It is valid to add a scalar to a 2 x 2 array, getting a 2 x 2 array. But it is not valid to add emptiness to a 2 x 2 array: you will get an error.
"my point is E will return value of B( when C > threshold) plus ones(2,2)."
You might mean one of two things there:
  1. That you want as output as many cells as in the original, that are the same size as the original, but in the cell entries corresponding to the cases where the threshold is reached, you want to add ones(2,2) to those entries; or
  2. That you want to use the threshold to select a subset of the B cells, and you want to add ones(2,2) to each member of the subset.
The second of those is easier to implement:
E = cellfun(@(B) B+ones(2,2), cellConfig.Boosts( cellfun(@(C) C>threshold, CellActivate) ), 'uniform', 0);
  1 个评论
Walter Roberson
Walter Roberson 2018-7-19
For the first of those,
mask = cellfun(@(C) C>threshold, CellActivate);
E = cellConfig.Boosts;
E(mask) = cellfun(@(B) B+ones(2,2), E(mask), 'uniform', 0);

请先登录,再进行评论。

更多回答(1 个)

Tiki Tiki
Tiki Tiki 2018-7-19
Thank.
I run your code. it is good. but because I plan to use vectorizing, so my output E should be same size with CellConfig.
so Can I use vectorizing here?

类别

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