Cell components perpendicular division

If I have a 4*4 cell array that each contain a 10*10 matrix as below (for example, Aii=10*10 matrix):
A={Aii Aij Aik Ail;
Aji Ajj Ajk Ajl;
Aki Akj Akk Akl;
Ali Alj Alk All};
How can I get a similiar cell array that denotes the division of perpendicular components such that I would get:
C={[Aii/Ajj] [Aij/Ajk] [Aik/Ajl] [Ail/Aji];
[Aji/Akj] [Ajj/Akk] [Ajk/Akl] [Ajl/Aki];
[Aki/Alj] [Akj/Alk] [Akk/All] [Akl/Ali];
[Ali/ij] [Alj/Aik] [Alk/Ail] [All/Aii]};

 Accepted Answer

Something like:
A = num2cell(reshape(1:16,[4 4])) % example array
B = cellfun(@rdivide,A,circshift(A,[-1 -1])) % output
To describe the shifting, consider the example:
A = {'Aii' 'Aij' 'Aik' 'Ail';
'Aji' 'Ajj' 'Ajk' 'Ajl';
'Aki' 'Akj' 'Akk' 'Akl';
'Ali' 'Alj' 'Alk' 'All'};
circshift(A,[-1 -1])
ans = 4×4 cell array
{'Ajj'} {'Ajk'} {'Ajl'} {'Aji'} {'Akj'} {'Akk'} {'Akl'} {'Aki'} {'Alj'} {'Alk'} {'All'} {'Ali'} {'Aij'} {'Aik'} {'Ail'} {'Aii'}

6 Comments

Great method! Thank you!
@MarshallSc, since all cells are the same size, why use a cell array at all? Why not use a simpler, faster, and more memory efficient 3-D or 4-D array?
@Image AnalystDo you mean using concatenation? I'm not very professional with Matlab, so I don't how I can use 3-D or 4-D arrays to do the pairwsie divisions. This process takes place multiple times by reshaping the original A cell, so I don't how to apply that method.
@DGM I have a quick question. When I use this method for my original cell, at first it gives me an error of:
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
And when I add an expression, it gives me the result. The expressions that I add are:
'UniformOutput',false & 'uniform',0
It seems that both work in the code. What are the differences between these two? Would it matter if I use either one?
Yeah, you might need to set uniformoutput depending on what's in the array.
There isn't a difference between false or 0 when setting 'uniformoutput'. A lot of things are implicit in Matlab where possible. false is explicitly a logical scalar. 0 is a numeric scalar, but it's treated implicitly as a logical false when used in a logical context (and all nonzero numeric values are treated as true). For example
thisvar = false;
if ~thisvar; fprintf('thisvar is false'); end
thisvar is false
thisvar = 0;
if ~thisvar; fprintf('thisvar is false'); end
thisvar is false
As to why it still works even when truncated to 'uniform', it's hard to say since cellfun() is a built-in and the code isn't visible. That said, a lot of the input parsing for other functions do often allow undocumented abbreviations of parameter names. In fact, it appears that the tests that are used are simply to test that the argument is a char vector matching the characters in the name 'uniformoutput'. The shortest char vector that would meet these requirements is 'un', which seems to work.
Thanks a lot for your great explanation!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 17 Jul 2021

Edited:

DGM
on 17 Jul 2021

Community Treasure Hunt

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

Start Hunting!