Hi there,
I am a beginner so the question maybe will be silly for you:
I have an cell 3x1 , where each element is an array
how can I select the first element, do some calculations and then the second one? (and so on..)
its a for loop but i cant find the proper syntax. thanks
Nikolas

4 comentarios

Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
hi again,
I have a cell like this [48X24 double];[48X24 double]; [48X24 double]
how can I extract each element [48x24] and do some calculations?
I mean to convert each element in a normal array [48x24], and by using a for loop, to make calculations with each of them. I asked also before but maybe my question wasnt fully understood. sorry many thanks
Nikola
Stephen23
Stephen23 el 20 de En. de 2017
Editada: Stephen23 el 20 de En. de 2017
@Nikolas Spiliopoulos: exactly like I showed you in my answer and comments.
for k = 1:numel(C)
C{k} % this is one array taken from C. Do calculations with it!
end
Where C{k} lets you either put something into cell k of cell array C, or take it out of that cell. That is all there is to it. There is nothing magic to do. If you are having problems then show us what you are doing and then we will be able to explain how to do it correctly. If you simply keep asking the same question you will keep getting the same answer. Because that is the answer. Because accessing data in cells of cell arrays is done using cell array indexing. Which you have been shown multiple times. Here it is again:
C{k}
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
i tried it, but I get an output like this = [1X24];[1X24];[1x24];
Stephen23
Stephen23 el 20 de En. de 2017
Editada: Stephen23 el 20 de En. de 2017
@Nikolas Spiliopoulos: That is a cell array containing all of your results.
What do you imagine should happen? What do you think MATLAB should do?
You stored the results in a cell array, and there they are, in a cell array. Exactly where you put them. My comments already showed you how to look at the values inside that cell array, if you want to do so.
Now that all of your results are stored in one cell array you could easily:
  • combine the separate numeric arrays into one numeric array.
  • loop over that output cell array and do further work: e.g. save the data, etc.
  • pass that cell array to other operations or functions.
  • etc
What happens next depends on your workflow. Which do you want to do?

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 20 de En. de 2017
Editada: Stephen23 el 20 de En. de 2017
C = {0:3, 4:5, 6:9};
out = NaN(size(C));
for k = 1:numel(C)
out(k) = mean(C{k});
end
or if the outputs are non-scalar, then put in a cell array:
out = cell(size(C));
for k = 1:numel(C)
out{k} = mean(C{k});
end

7 comentarios

Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
thanks for the answer, to make it more clear I need to take each element from the cell and use it as an ordinary array. but only one at a time thanks again
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
i tried this though and i get an error "In an assignment A(:) = B, the number of elements in A and B must be the same"
Niels
Niels el 20 de En. de 2017
your question has alrdy been answered 1 hour ago...
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
i just get the final array for i=3! i need to get all of them one by one and save them somewhere else..
Stephen23
Stephen23 el 20 de En. de 2017
Editada: Stephen23 el 20 de En. de 2017
@Nikolas Spiliopoulos: my code does not use i, so I have not idea what code you are talking about. My code also returns all of the values correctly, not just the last one:
C = {0:3, 4:5, 6:9};
out = cell(size(C));
for k = 1:numel(C)
out{k} = mean(C{k});
end
produces this output:
>> out{1}
ans =
1.5
>> out{2}
ans =
4.5
>> out{3}
ans =
7.5
If you have written your own code then it has a mistake in it. My code returns all of the values calculated during the loop. To save them read this:
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
ok thanks a lot,
i"ll try again!
Stephen23
Stephen23 el 20 de En. de 2017
@Nikolas Spiliopoulos: remember to accept the answer that helps you most.

Iniciar sesión para comentar.

Más respuestas (2)

Niels
Niels el 20 de En. de 2017
Editada: Niels el 20 de En. de 2017
Hi,
with {n} you get access to the nth element
>> a={[1 2] , 'abcdefg', randn(4)}
a =
1×3 cell array
[1×2 double] 'abcdefg' [4×4 double]
>> a{1}
ans =
1 2
or in a loop:
for i=1:3
b=a{i}; % you dont need to declare a new variable
% you can also work with a{i}
>> a{3}(1:2,2:3)
ans =
0.3188 3.5784
-1.3077 2.7694
end
this documentation might help you as well:
Nut
Nut el 20 de En. de 2017
Editada: Nut el 20 de En. de 2017
Hi,
I think the following example could be a basic help, is it useful for you?
% 3x1 cell building (example)
cellarray{1} = [1 2 3];
cellarray{2} = [4 5 6];
cellarray{3} = [7 8 9];
cellarray = cellarray';
% Get the second array
cellarray{2}
% Get the first element of the third array
cellarray{3}(1)
% Get the first cell element
cellarray(1)

2 comentarios

Nikolas Spiliopoulos
Nikolas Spiliopoulos el 20 de En. de 2017
yeah its ok, however how can I use the for loop with that in order to choose one element(array) a time? thanks
Nut
Nut el 20 de En. de 2017
Editada: Nut el 20 de En. de 2017
I'm thinking to something like:
for i = 1:numel(cellarray)
array = cellarray{i};
% Do your calculations using "array"
end

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 20 de En. de 2017

Editada:

Nut
el 20 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by