Store Variables from for loop to cell array. it only stores the last run

3 visualizaciones (últimos 30 días)
hello I want to store the variables from a 10x75 matrix, to a cell array. but it should not start at (1,1) in the cell array, it has to start at cell (5,2). If I run the code, it'll only store the variables from how_many_NaNs(:,75), to result(:,79). All the other cells are filled with []. Does anyone has an idea how to fix that problem? Thanks for you advice.
cnter = 2
%create header
for j = 1:size(marker,1)
result(1,4+j) = {marker(j,:)};
end
%store variables
for n = 1:size(how_many_NaNs,1)
result(cnter,4+j) = {how_many_NaNs(n,j)};
cnter = cnter +1;
end
end
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 25 de Mzo. de 2016
You can make your question clear with a small example.
Oliver Kumar
Oliver Kumar el 26 de Mzo. de 2016
hi azzi
just solved the problem. there was a misstake in one line of code...
kind regards

Iniciar sesión para comentar.

Respuestas (1)

Joseph Cheng
Joseph Cheng el 25 de Mzo. de 2016
Editada: Joseph Cheng el 25 de Mzo. de 2016
it might be obvious but you might want to nest your for loops.
cnter = 2
%create header
for j = 1:size(marker,1)
result(1,4+j) = {marker(j,:)};
end
%store variables
for n = 1:size(how_many_NaNs,1)
result(cnter,4+j) = {how_many_NaNs(n,j)};
cnter = cnter +1;
end
end
here is your original code and it says you'll be running j from 1 to size of marker. then at the end of the j for loop: j = size(marker,1).
then you go into your n for loop with j=size(marker,1). without understanding the data itself to see how_many_NaNs is made up of, wouldn't you want to go through all j values for each n?

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by