# How do I make a cell of cells in a for loop?

1 view (last 30 days)
Samuel Davies on 20 Jan 2015
Commented: Samuel Davies on 23 Jan 2015
Here is the code I am running, I need an explanation on why this doesn't work and any suggestions on how to fix it. Thank you.
for k = 1:1:Ni
for bt = 1:1:(Lt/2)
omt1(bt,:)={xtu(2*bt-1,1),ptu(2*bt-1,k),ztu(2*bt-1,1),xtu(2*bt,1),ptu(2*bt,k),ztu(2*bt,1)};
end
end
xtu is a 202 x 1 column vector, ptu is a 202 x 8 matrix and ztu is a 202 x 1 vector. I take the even and odd entries to make the cell of 101 rows and 6 columns as seen in the for loop. Ni is an arbitrary input but let it be 8, Lt is equal to 202, as such bt = 1:1:101, omt1(bt,:) produces the last loop (k = 8) with omt1 being a cell made up of 101 rows x 6 cells.
What I want is for omt1 to be a cell made up of these 8 101 x 6 cells and I am stuck on this. Putting the : in omt1 as k so that I get a cell of 8 columns, each column being a cell of 101 x 6 does not work, why? I think it is because I have not said explicitly that omt1 is to be a cell of cells. At the moment it is just a cell of 101 x 6. How can this be done to put k across grouped as a cell? Thank you.

Niels on 20 Jan 2015
Your variable omt1 is put inside the wrong loop. Right now it is overwritten for every iteration of k.
What you should do instead is create a k-by-1 cell array for omt1 and (literally) give those 101x6 matrices as input at every k.
Something like this should do the trick:
omt1 = cell(8,1);
for k = 1:1:Ni
omt1{k} = [xtu(1:2:end), ptu(1:2:end,k), ztu(1:2:end), xtu(2:2:end), ptu(2:2:end,k), ztu(2:2:end)];
end

Samuel Davies on 22 Jan 2015
It is a single column matrix, so really a vector. I just like working with everything in column vectors so I use x(:,1) to give that format.
Niels on 22 Jan 2015
So actually you are comparing a large vector xt with every single value in xtu. If a value turns out to be more than once in xt, then you want only the first index to be kept in xt and also in yt? I suppose this also means that xtu contains only unique values.
In that case it's much easier to just remove those indices at the moment you find them. One approach you could use here is by determining the unique values in xt, e.g.:
[vals, locs] = unique(xt,'stable');
The indices of the values that are non-unique can then be found with e.g.
idxToRemove = setdiff(1:length(xt), locs);
Of course this is under the assumption that you want to do the same if a number occurs more than twice or if there are no such cases. Furthermore I did not take into account non-unique numbers that do not occur in xtu, as I don't know whether these exist and what you want to do with them. But it shouldn't be too hard to exclude these indices (or include them if you don't want to keep them at all).
Samuel Davies on 23 Jan 2015
Exactly, xtu is unique(xt,'stable'); I just set this:
xtu = unique(xt,'stable'); % Identify all the unique xt and store in xtu
xtis(:,1) = 0; % Initialise xtis (x top index store) counter
for i = length(xtu):-1:1 % Count down from the last entry
idxti = find(xt(:,1) == xtu(i,1)); % Identify the rows
% corresponding to i'th unique xtu in xt
if length(idxti) == 2 % Check that we have two of the same xt
% equal to xtu
xtis(:,1) = xtis(:,1) + 1; % Add one to the counter
end
end
xtui = cell(xtis(:,1),1); % Make a cell that is the number of double
% indices long
k = length(xtui(:,1)); % Initialise k index
for i = length(xtu):-1:1 % Count down from the last entry
idxti = find(xt(:,1) == xtu(i,1)); % Identify the rows
% corresponding to i'th unique xtu in xt
if length(idxti) == 2 % Check that we have two of the same xt
% equal to xtu
xtui{k,:} = idxti(1,1); % Assign values to the cell of the
% double indices indexes
k = k - 1; % Take one from the k index
end
end
xtui = cell2mat(xtui); % Convert cell to matrix
Yot = {yiat;coscurvet;ycct;sincurvet;yoat}; % y out top
yt = cell2mat(Yot); % Convert cell to matrix
ytu = yt; % Store ytu as yt
ytu(xtui) = []; % For each index in xtui remove the value in ytu
So I found the indices if the value in xt that was equal to xtu occurred twice, stored it in a vector and just took ytu(that indice) = []; and removed in from ytu. I will play around with your solution so that I understand it.
I won't get a value occurring more than twice in the scheme I want to set up. It happens when I join two matrices, the end and start of another have to be the same, I was just trying to concatenate and smooth the points to be continuous like 1..2..2..3 ---> 1..2..3. As for numbers occurring outside of xtu to be found in xt is not possible is it using the unique command? I am not sure. As always thank you for the help.