short cut to subtract cells without replicating numbers
Show older comments
Hi guys, assume the following: Cell array mx (100x61), Cell array mn (100x61), Cell array a (100x61).
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 (100x61), Cell array mn (100x61), Cell array a (100x61).
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.
4 Comments
Image Analyst
on 26 Nov 2014
I don't understand your non-standard terminology despite reading it several times. A matrix is (almost always) a 2D array. An array can be an array of any dimension : 1, 2, 3, 4 or whatever. Your cell arrays (mx, mn, and a) are matrices of cells because they are 2D arrays of individual cells. A "table" is a special type of variable in MATLAB where each column is one type of variable, but the different columns can be different data types. A cell is like a container that can hold any type of variable - think of it like a bucket, and read this for a better understanding. Cells can hold any variable type and each row and column doesn't have to be the same - it can vary on a cell by cell basis. You refer to "a" as a cell array sometimes, and a single "matrix table" at other times. Yet you also say "These cell arrays consists of multiple matrix tables." So now, you say "a" is several "matrix tables" (whatever those are) instead of one "matrix table." In your mind, what is a matrix table?. I'll assume "a" is a cell array that is a matrix of 100 rows of 61 columns where each element is a single cell. Now, what is in each cell? Is each cell that Kron/ones thing? And do you really need cells instead of a standard numerical multidimensional array?
AA
on 26 Nov 2014
AA
on 26 Nov 2014
This seems to be a good example of where the OP is asking about solving a particular issue (something regarding loops and the Kronecker tensor product), but does not actually address their basic intention. The statements made, eg " a is a cell array (100x61) where each element is a single cell." indicate some basic misunderstanding of cell arrays (as based on the code they gave in the above comment, all of a, mn and mx are actually 1x1 cell arrays containing numeric arrays), so perhaps some loose interpretation is required of the problem...
Thus it seems that those cell arrays and for-loops are a red-herring: it appears that the question is essentially "how do I replicate an array X so that it is the same size as another array Y, thus allowing operation fn(X,Y) to be performed". Important is perhaps the request "...without replicating the actual rows": perhaps bsxfun would be a suitable solution? Certainly this could be done without resorting to loops and the like...
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!