필터 지우기
필터 지우기

Breaking up a matrix/array

조회 수: 5 (최근 30일)
Andrew Kreitzman
Andrew Kreitzman 2013년 6월 24일
댓글: Megan Renny 2018년 7월 10일
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