transforming a script for a matrix to a cell array

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

Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
Note that a table is another data class in MATLAB, and is neither a cell array nor a numeric array .
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
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?
AA
AA 2014년 12월 2일
편집: AA 2014년 12월 2일
consider the following example:
a cell array {1x3}
3755940x7 double 1878000x7 double 1252020x7 double
and the cell arrays {3x61} for mx/mn values with 61 matrices of the dimension (62599x1 double), 61 matrices of the dimension (31300x1 double) and 61 matrices of the dimension (20867x1 double).
b = reshape(a(:,7),60,[])
---> I want all the matrix columns in cell array a to be reshaped into blocks of 60
q = bsxfun(@minus,b,mx)
q = bsxfun(@rdivide,q,mx-mn);;
---> The question is which mx/mn matrix should be used for b? The answer is that the number of rows of the matrices in cell array mx/mn multiplied by 60 corresponds to the number of rows of the matrices in cell array a. 62599 times 60 equal=3755940x7 double. So the matrices with 62599 rows deal with the matrices that have 3755940 rows. The output should be a cell array in the format cell arrays {3x61}
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
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?
Why must the numeric arrays in a contain six unused columns?
AA
AA 2014년 12월 2일
The six unused columns are not needed but they are in my original work and this is why I included them. they could be deleted.
From cell array a I just need column 7 from each matrix in each cell. I want to perform the operation on a scalar of the small array. So the output is in a cell array format of {3x61}. The output should look like the picture below. I created a table with excel to show how the cell array should look like.
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
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).
AA
AA 2014년 12월 2일
편집: AA 2014년 12월 2일
No we dont run out of a.
Consider the following
F(a(x,y),mn(x,y)).
a(1,1)-mn(1,1) a(1,1)-mn(1,2) a(1,1)-mn(1,3) ... a(1,1)-mn(1,61)
a(1,2)-mn(2,1) a(1,2)-mn(2,2) a(1,2)-mn(2,3) ... a(1,2)-mn(2,61)
a(1,3)-mn(3,1) a(1,3)-mn(3,2) ... a(1,3)-mn(3,61)
a(x) always stays 1
a(y) =mn(x)
y=1 to 61
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
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
AA 2014년 12월 2일
편집: AA 2014년 12월 2일
no a is 1 x 3 cell array. i made a mistake in my initial post. consider transpose (a = { rand(6000,7); rand(3600,7); rand(600,7))
maybe something like this
q = a;
n=61
for f=1:n
for k = 1:numel(q)
b = reshape(a{k}(:,7),60,[]).';
b = bsxfun(@minus,b,mx{k,n});
b = bsxfun(@rdivide,b,mx{k,n}-mn{k,n});
q{1} = reshape(b.',[],1);
end
end
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
Yes, basically you need to take the answer I gave, and wrap with another loop. I will reply to my original answer with some code that you could try.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
It seems that you want to apply a function to multiple numeric arrays contained in several cell arrays. There are essentially two ways to achieve this:
  • Use a loop over the length of the arrays.
  • Use cellfun .
Note that a loop can be achieved in a script, whereas cellfun would (most likely) require a function to be defined. cellfun can be a tidy solution, as it allows local code to show intent by defining a complicated function elsewhere. In any case, the following code will apply that function to those cell arrays, using loops:
a = {rand(6000,7); rand(3600,7); rand(600,7)};
mx = {randi(50,600,1); randi(50,360,1); randi(50,60,1)};
mn = {randi(50,600,1); randi(50,360,1); randi(50,60,1)};
q = a;
for k = 1:numel(q)
b = reshape(a{k}(:,7),10,[]).';
b = bsxfun(@minus,b,mx{k});
b = bsxfun(@rdivide,b,mx{k}-mn{k});
q{k} = reshape(b.',[],1);
end
Note that this code includes several corrections to what you supplied above, as the dimensions of randi(360,60,1) will lead to an error. I also uncommented the last two lines of the algorithm, as these are required to match the function that you gave in an earlier question.

댓글 수: 8

Thanks, I made a mistake in supplying the wrong information about the mx/mn.
consider the following example:
a cell array {1x3}
3755940x7 double 1878000x7 double 1252020x7 double
and the cell arrays {3x61} for mx/mn values with 61 matrices of the dimension (62599x1 double), 61 matrices of the dimension (31300x1 double) and 61 matrices of the dimension (20867x1 double).
b = reshape(a(:,7),60,[])
---> I want all the matrix columns in cell array a to be reshaped into blocks of 60
q = bsxfun(@minus,b,mx)
q = bsxfun(@rdivide,q,mx-mn)
---> The question is which mx/mn matrix should be used for b? The answer is that the number of rows of the matrices in cell array mx/mn multiplied by 60 corresponds to the number of rows of the matrices in cell array a. 62599 times 60 equal=3755940x7 double. So the matrices with 62599 rows deal with the matrices that have 3755940 rows. The output should be a cell array in the format cell arrays {3x61}
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
The statement here is highly ambiguous:
b = reshape(a(:,7),60,[])
---> I want all the matrix columns in cell array a to be reshaped into blocks of 60
  • in this example, a is not a cell array, so it makes little sense in the context to talk about reshaping cell arrays.
  • the code a(:,7) produces an Nx1 array (i.e. a column vector), so there is only one column. Which "all the matrix columns" are you referring to?
If we take a as the 600x9 numeric of your original question, then of course the columns can be manipulated at every sixtieth element. The question is, how? We can split the numeric into a cell array, reshape it, or perform many other shape manipulations... please advise!
AA
AA 2014년 12월 2일
ok I will think about this and come back to you.
This works, with cell arrays mx and mn of both of size 3x2, and a of size 3x1. As you mentioned, tranpose might be useful, but keep an eye on the size functions if you change the array orientation.
a = {rand(6000,7); rand(3600,7); rand(600,7)};
mx = {randi(50,600,1), randi(50,600,1); randi(50,360,1), randi(50,360,1); randi(50,60,1), randi(50,60,1)};
mn = {randi(50,600,1), randi(50,600,1); randi(50,360,1), randi(50,360,1); randi(50,60,1), randi(50,60,1)};
q = mx;
for x = 1:size(q,1)
for y = size(mx,2)
b = reshape(a{x}(:,7),10,[]).';
b = bsxfun(@minus,b,mx{x,y});
b = bsxfun(@rdivide,b,mx{x,y}-mn{x,y});
q{x,y} = reshape(b.',[],1);
end
end
AA
AA 2014년 12월 2일
편집: AA 2014년 12월 2일
b must be a cell array or otherwise some cells will be lost in a.
b{x,y}= reshape(a{x}(:,7),10,[]).';?
Stephen23
Stephen23 2014년 12월 2일
편집: Stephen23 2014년 12월 2일
Why does b need to be a cell array? On every loop b gets completely redefined and its values are assigned to q. There is no need to store its data in a cell array, unless you particularly need to have these intermediate value later...
Which cells are "lost" ? Can you indicate which ones are concerning you?
AA
AA 2014년 12월 2일
great i didnt know that. thanks for your help. really appreciate it.
Glad to be able to help. I hope you feel more comfortable with loops and arrays now :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

AA
2014년 12월 2일

댓글:

2014년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by