Error Using cellfun for a Matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Simon Chan
2022-1-10
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 个评论
更多回答(1 个)
Kevin Holly
2022-1-10
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 个评论
Kevin Holly
2022-1-10
编辑:Kevin Holly
2022-1-10
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!