How do I make a cell of cells in a for loop?
2 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Niels
2015 年 1 月 20 日
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 件のコメント
Niels
2015 年 1 月 22 日
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).
その他の回答 (0 件)
参考
カテゴリ
Help Center および 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!