Error Using cellfun for a Matrix
1 view (last 30 days)
Show older comments
I have an 81 x 81 matrix that divided it into smaller matrices:
a = rand(81,81);
n = 3*ones(1,27);
a_d = mat2cell(a,n,n)
Now I want to perform this operation on each of the small matrices but it gives me error:
G_x = @(x) sum((x-a_d)./(x+a_d),'all');
B_x = cellfun(G_x,a_d)
Can someone please help me! Thank you.
0 Comments
Accepted Answer
Simon Chan
on 10 Jan 2022
Try the following:
for r = 1:27
for c = 1:27
A = a_d{r,c};
B_x{r,c} = cellfun(@(x) sum((x-A)./(x+A),'all'),num2cell(A));
end
end
0 Comments
More Answers (1)
Kevin Holly
on 10 Jan 2022
a = rand(81,81);
n = 3*ones(1,27);
a_d = mat2cell(a,n,n);
for i = 1:27
for j = 1:27
G_x = @(x) sum((x-a_d{i,j})./(x+a_d{i,j}),'all');
B_x(i,j,:,:) = cellfun(G_x,a_d);
end
end
size(B_x)
2 Comments
Kevin Holly
on 10 Jan 2022
Edited: Kevin Holly
on 10 Jan 2022
FYI, braces needed to be added as Simon had done.
a = rand(81,81);
n = 3*ones(1,27);
a_d = mat2cell(a,n,n);
for i = 1:27
for j = 1:27
G_x = @(x) sum((x-a_d{i,j})./(x+a_d{i,j}),'all');
B_x{i,j,:,:} = cellfun(G_x,a_d); %Added braces here
end
end
size(B_x)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!