for loop concatenation for datasets
7 次查看(过去 30 天)
显示 更早的评论
I am working on a code that combines datasets of equal sizes.
I want to create a loop that concatenates the datasets horizontally. In the loop below ds_temp will be recurrring datasets (200x2) that will need to be combined to form the final dataset ds. Can you please help?
ds = ds_temp;
ds = horzcat(ds,ds);
end
2 个评论
Sven
2012-2-25
Joe, the above code (first 2 lines) does not error. Can you clarify your actual problem? Is it that you're not sure of the syntax for making a loop?
采纳的回答
Sven
2012-3-3
Hi Joe,
The following loop:
- makes some random data (20-by-2)
- initialises a variable called allData to start as 20-by-2
- concatenates that random data onto allData, 5 times.
randomData = rand(20,2);
allData = randomData;
for i = 1:5
dataToAppend = randomData + i;
allData = [allData dataToAppend];
% allData = cat(2, allData, dataToAppend);
% allData = horzcat(allData, dataToAppend);
end
Note the two lines commented out. They do the exact same thing, just with slightly different syntax.
The end result will be a 20-by-12 matrix, because allData started at 20-by-2, and had 2 columns added to it 5 times.
1 个评论
Johanna
2018-2-28
Um, might be just me, but this way you are also adding the value of i to randomdata on each loop? Surely that's not desired...
更多回答(1 个)
Yesid Goyes
2013-6-10
How do I automatically concatenate several pages of the same multidimensional array.?
if true
% cc=cat(1,xyz(:,:,1),xyz(:,:,2),xyz(:,:,3),...,xyz(:,:,n));
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!