MATLAB: Short cut to subtract cells without replicating numbers

replication subtraction

Hi guys, assume the following: Cell array mx (100×61), Cell array mn (100×61), Cell array a (100×61).
These cell arrays consists of multiple matrix tables. Matrix table 'a' has 7 columns and matrix table mx/mn have one column. I want to carry out the following calculation between the corresponding matrix tables in the cell array: (a{column7row n}-mn{n})/(mx{n}-mn{n}). n=n+1 which increases after each row.
For that purpose I have been given this formula on the forum: Hi guys, assume the following: Cell array mx (100×61), Cell array mn (100×61), Cell array a (100×61).
These cell arrays consists of multiple matrix tables. Matrix table a has 7 columns and matrix table mx/mn have one column. I want to carry out the following calculation between the corresponding matrix tables in the cell array: (a{column7row n}-mn{n})/(mx{n}-mn{n}). n=n+1 which increases after each row.
For that purpose I have been given this formula on the forum:
q = (a{1}(:,7)-mx{:})./(mx{:}-mn{:});
The formula is great but for that formula to work I had to replicate the numbers in the matrices of the cell array mx and mn with the following formula:
n=61
for f = 1:n
for k=1:length(mx)
mx{k, f} = [kron(mx{k, f}, ones(60, 1))];
end
end
n=61
for f = 1:n
for k=1:length(mn)
mn{k, f} = [kron(mn{k, f}, ones(60, 1))];
end
end
The problem is that this process uses a lot of memory. Is there a short cut to carry out the calculation without first replicating the cells.The replication was necessary as each matrix in the original mn/mk had 60 times less rows than in the matrices of cell array a, i.e. when mn{1,1} had 6000 rows then the corresponding cell in a{1,1} had 360000. So in order to carry out a calculation I had to replicate the rows in mn first. I want the calculation to be carried out with the corresponding replicated rows without replicating the actual rows.

Best Answer

Based on my comment above (that the cell arrays and loops are red-herrings), then this simplifies down to "how to replicate the arrays mn and mx to the same size as a, then apply some function to them all". Of course, the answer is to not replicate them, but to use bsxfun instead:
a = randi(50,600,9);
mx = randi(50,10,1);
mn = randi(50,10,1);
q = bsxfun(@minus,reshape(a(:,7),10,[]),mx);
q = bsxfun(@rdivide,q,mx-mn);
q = reshape(q,[],1);
Note the last three lines are derived from the given function:
q = (a{1}(:,7)-mx{:})./(mx{:}-mn{:});
after removing the (useless?) cell array references. To get exactly the same behavior as the OP's code, it might be required to perform some nonconjugate transpose operations at some steps.