transforming a script for a matrix to a cell array
Show older comments
a = randi(50,600,9);
mx = randi(50,60,1);
mn = randi(50,60,1);
b = reshape(a(:,7),10,[]).';
q = bsxfun(@minus,b,mx);
%q = bsxfun(@rdivide,q,mx-mn);
%q = reshape(q.',[],1);
How can I apply this code which has been designed for a matrix table to a cell array?
Imagine the following
a = { rand(6000,7); rand(3600,7); rand(600,7); };
mx = {randi(50,600,1); randi(360,60,1); randi(50,60,1)}
mn = {randi(50,600,1); randi(360,60,1); randi(50,60,1)}
12 Comments
Note that a table is another data class in MATLAB, and is neither a cell array nor a numeric array .
It has also not yet been explained why you need to create the numeric matrices of a with seven (in previous questions with nine) columns, but then only ever refer to one of the these columns. Why create this extra data which does not then get used?
In your question you give examples of cell arrays a, mx and mn, all of which have dimensions 3x1.
Now you state that a should be 1x3, and both of mx and mn should be 3x61.
I understand that the numbers of rows of the numeric arrays contained within mx and mn is a divisor of those within a. We dealt with this in an earlier question. However we now have a different issue: what kind of operation do you wish to perform on the arrays a, mn and mx ? Currently these cell arrays are all different sizes... do we ignore the extra elements, replicate the smaller arrays, or perform our operation on a scalar of the small array but an array-subset of the larger array?
Stephen23
on 2 Dec 2014
Why must the numeric arrays in a contain six unused columns?
AA
on 2 Dec 2014
Okay, so the six unused columns are not a problem for you :)
Lets sort out the cell array sizes: The most recent definition is that a is 1x3, and mx and mn are each 3x61. Usually one tries to operate on corresponding elements (ie F(a(x,y),mn(x,y)), but as you can see if x>1 or y>3 then we run out of a, as it is simply not the same size as mx or mn.
It is not clear to me why you need the cell arrays at all. I have a feeling that these might be making the issue more complicated than it needs to be... it really would be much easier to create the results using numeric arrays, and split it into a cell array afterwards (iff required).
So a is actually size 3x1, and not 1x3 as you stated earlier?
This might seem like a pedantic point, but MATLAB was primarily written by (and for) mathematicians, and in mathematics, array dimensions really matter...
This is quite different to some languages, where they will assume that you want to match dimensions that seem to be similar... this is simply not strict enough for mathematicians ;)
AA
on 2 Dec 2014
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!