Is there anyway to speed this code up ??
Show older comments
Basically, the case with me is that I have a large set of data with only 4 columns. I currently have a dataset array and I want to separate the observations. I have indexed the different groups of observations by 1, 2, 3, ..... 12984. All of these are currently stored in a dataset array. What I want to do is to create a numerical matrix that consists of columns for the 12984 different observations as well as their different rows since I can operate on it faster and more efficiently and have repeated observations. The problem is some of them are all unique and so some of the groups of observations have for instance (616,3), (500,3) etc etc.
This is the code I have so far:
mat = ones(200,3);
i = 1;
while i <= 10
c = ds(ds.newid == i, {'permno','monthlycumlnret','dates',});
d = cat(1, [ double(c) ]);
if length(d) == length(mat);
mat = [mat d];
if length(d) > length(mat)
z = ones(length(d)-length(mat),3);
mat = [vertcat(1,z,mat) d];
else
z = zeros(length(mat)-length(d),3);
mat = [mat vertcat(1,z,d)];
end
i = i + 1;
end
end
I haven't even done it up to 100 and it is already taking for ever to run. Can someone please help me? I am a bit new at matlab so any help is appreciated.
5 Comments
Stephen23
on 15 Jan 2016
Can you please upload some sample data that we can try this code with. Simply click the paperclip button, then both the Choose file and Attach file buttons.
Putsandcalls
on 15 Jan 2016
Your code is buggy, in particular by constructing an infinite loop. Your code never runs anything inside the if statements, because the first condition is never met, i is never incremented, and so on the next iteration it starts from anew again. Bingo, one infinite loop. Your code only runs this:
mat = ones(200,3);
i = 1;
while i <= 10
c = ds1(ds1.newid == i, {'permno','monthlycumlnret','dates',});
d = double(c);
if length(d) == length(mat);
... nothing here ever runs!
end
end
because
K>> length(d)
ans =
703
K>> length(mat)
ans =
200
This was easy for me to check using MATLAB's debugging tools. Learn how to use them.
Although "other languages" depend on loops to solve everything, MATLAB is not "other languages" and your should learn that to write vectorized code, such as Guillaume's answer.
Putsandcalls
on 15 Jan 2016
Guillaume
on 15 Jan 2016
In addition, do not use length on 2D arrays. If your arrays has less rows than columns, length will return the number of columns, which is not what you want in your code above.
Always be explicit. You want the number of rows, so use
size(mat, 1)
Accepted Answer
More Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!