Breaking up a matrix/array

5 次查看(过去 30 天)
I am running a regression of some global equity data over various style factors, however my return data is conglomerated into one 30,000x2 matrix....One column is the "country code) of the security and the other is the return(obviously). Is there a way to break up this returns matrix into many smaller matrices based on the country.I thought about using a for loop with an if statement like: for i = 1:length(country_codes) if country_codes(i) == country_codes(i+1)
And then I get stuck....it doesnt seem reasonable to use a for loop where I will be creating a new matrix inside the loop, possily breaking out of the loop to store the matrix, and then starting where I left off. There are about 50 countries, so you can see how this would be terribly inefficient (if at all possible). Also, ideally I would store the indices of the new arrays of country codes so that I could use these indices to concatenate the new country code and return arrays with the corresponding style factor weights and industry code data for the regression. Hopefully someone has a good idea! Thanks!

采纳的回答

Iain
Iain 2013-6-24
codes = matrix(:,1);
code_list = unique(codes);
for i = 1:numel(code_list)
new{i} = matrix(code_list(i) == codes,:);
end
  6 个评论
Andrew Kreitzman
Andrew Kreitzman 2013-6-24
Never mind! Boy are cells useful! Thanks for the help.
Megan Renny
Megan Renny 2018-7-10
Hello, I have a similar issue. I have a program that reads in data and creates a vertical cell of matrices. The cell will not always be the same height when this function is used. I am trying to find out how to create another script or function that will simply pull out each array out of the cell, and make it its own variable. I have seen that there are a lot of "incorrect" ways to do this, but I am only trying to use this to read in data in an easily plotted way.
Ie. The raw txt file will have 6 columns and a many rows. The first program a friend wrote identifies the beginning and end of each experimental run and divides this into multiple arrays that are stored in a common cell that has a height equal to the number of experiment's I've done. Each array may have a different height.
I want to now have each array, or element of the cell, be its own variable. How might this be done? Thank you in advance for your kind response.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by