Inverse of a cell array

2 views (last 30 days)
MarshallSc
MarshallSc on 6 Jul 2021
Answered: the cyclist on 6 Jul 2021
Hello, I'm trying to get the inverse values of a 10*10 cell array, each containing a 4*4 matrix. The code is (credit to Stephen Cobeldick for the code):
x1 = rand(10,10);
y1 = rand(10,10);
z1 = rand(10,10);
r1 = rand(10,10);
a1 = cat(3,x1,y1,z1,r1);
x2 = rand(10,10);
y2 = rand(10,10);
z2 = rand(10,10);
r2 = rand(10,10);
a2 = cat(4,x2,y2,z2,r2);
tmp = num2cell(sqrt(a1./a2),3:4);
fun = @(a)reshape(a,4,4);
out = cellfun(fun,tmp,'uni',0);
Now I want the inverse of the results (1/values). I used some commands but the results are not accurate. For example using:
out2=cellfun(@inv, out, 'uniform', 0);
But the results are not correct. Is there any method that can calculate the inverse of the cell array? Thank you.

Accepted Answer

the cyclist
the cyclist on 6 Jul 2021
Do you mean you want the reciprocal of each element of each matrix? (That is not what the inverse of a matrix means.)
If so, then you want
x1 = rand(10,10);
y1 = rand(10,10);
z1 = rand(10,10);
r1 = rand(10,10);
a1 = cat(3,x1,y1,z1,r1);
x2 = rand(10,10);
y2 = rand(10,10);
z2 = rand(10,10);
r2 = rand(10,10);
a2 = cat(4,x2,y2,z2,r2);
tmp = num2cell(sqrt(a1./a2),3:4);
fun = @(a)reshape(a,4,4);
out = cellfun(fun,tmp,'uni',0);
out2=cellfun(@(x) 1./x, out, 'uniform', 0);
% example of one reciprocal:
out{1}
ans = 4×4
0.5451 0.3943 0.6951 0.5691 1.1282 0.8162 1.4386 1.1779 0.1688 0.1221 0.2152 0.1762 0.7559 0.5469 0.9639 0.7893
out2{1}
ans = 4×4
1.8345 2.5359 1.4387 1.7571 0.8864 1.2252 0.6951 0.8490 5.9252 8.1905 4.6467 5.6751 1.3228 1.8286 1.0374 1.2670

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!