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

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.

 采纳的回答

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

6 个评论

Hi Niels,
Thank you for your answer, it worked perfectly. Now I am trying to modify it like this:
xtis(:,1) = 0; % Initialise xtis counter
for i = length(xtu):-1:1
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
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
for i = length(xtu):-1:1
for k = length(xtui):-1:1
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
xtui{k,:} = idxti(end,1);
end
end
end
This code above is a modification of another answer I saw while surfing MATLAB central. I cycle through xt and find where it is equal to xtu in doublets and count the number of times this occurs. A cell is then made of the length of occurrences. What I want then is to cycle through again and when a doublet occurs, place the index into another cell so that I end up with a cell of the doublet indexes, in this case I have doublets occurring in xt at (2,3),(102,103),(104,105),(204,205) for example with the final cell being {3,103,105,205}. I will then do a further modification to take away the y values in the doublet positions from the yt you saw previously. It saves me having to type out the cell currently as it is in indexes, missing the second of the doublets, this way I can make the final cell arbitrary in length. If this is not clear then I will recomment and explain further. Thank you.
A very inelegant solution that I am not sure will have problems later if I change the program again. I just put k = 4 after the cell but before the second for loop and took 1 away in the if statement. It works but maybe you can show me a more slick solution with a loop or some other method. Thank you.
I see you compare xt(:,1) in every cycle of your loop. What are the dimensions of xt in your snippet? Is it N-by-1? Or is it a matrix where only the first column matters?
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.
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).
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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by