필터 지우기
필터 지우기

for loop concatenation for datasets

조회 수: 3 (최근 30일)
Joe
Joe 2012년 2월 25일
댓글: Johanna 2018년 2월 28일
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
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?
Joe
Joe 2012년 3월 3일
Thanks Sven for your response. You are right the code does not error. I am not sure of the syntax for making a loop as I am still learning to code! Each dataset has two columns - date and prices. I am able to get data for each datasets but the loop is currently overwriting all dataset and keeps the last dataset. I meed to save the first dataset and add all recurring datasets to the original dataset. I can also use the join function instead of concatenation to add all new datasets. I am just not sure of the syntax!

댓글을 달려면 로그인하십시오.

채택된 답변

Sven
Sven 2012년 3월 3일
Hi Joe,
The following loop:
  1. makes some random data (20-by-2)
  2. initialises a variable called allData to start as 20-by-2
  3. 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
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
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

카테고리

Help CenterFile 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