transforming a script for a matrix to a cell array

2 Ansichten (letzte 30 Tage)
AA
AA am 2 Dez. 2014
Kommentiert: Stephen23 am 2 Dez. 2014
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 Kommentare
AA
AA am 2 Dez. 2014
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 am 2 Dez. 2014
Bearbeitet: Stephen23 am 2 Dez. 2014
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Dez. 2014
Bearbeitet: Stephen23 am 2 Dez. 2014
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 Kommentare
AA
AA am 2 Dez. 2014
great i didnt know that. thanks for your help. really appreciate it.
Stephen23
Stephen23 am 2 Dez. 2014
Glad to be able to help. I hope you feel more comfortable with loops and arrays now :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by