Divide matrix in to vectors using for-loop

조회 수: 13 (최근 30일)
Olle Haglund
Olle Haglund 2017년 9월 21일
댓글: Stephen23 2017년 9월 22일
So, i have a 12*X matrix where each row represents a month (jan-dec) and each column represents a year (could be any amount of years). I want to divide these into separate vectors of months and years using a for-loop.
This is my thought on how to do it but it doesnt work, basically i want to create variables for each year where y1 represents year 1, y2 represents year 2 and so on. I get a error message as soon as i try to run this and i think the problem is in the y(i) part which is supposed to make the different variables.
for i=1:length(x)
y(i)=x(:, i);
end
  댓글 수: 1
Guillaume
Guillaume 2017년 9월 21일
Creating several variables from one matrix = a very bad idea.
In all likelyhood, even splitting the matrix into a cell array or similar is also a bad idea. If you're trying to obtain statistics per month/year/whatever keeping the data together in the matrix would be much simpler. Tools like findgroups and splitapply can let you get monthtly/annually/etc. stats.

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

채택된 답변

OCDER
OCDER 2017년 9월 21일
편집: OCDER 2017년 9월 21일
Almost had it right. Use cell instead of y1, y2, y3, etc. Your "y1" can be accessed as y{1}. This is much simpler than naming many variables y1, y2, etc.
Also, use size(x, 2) instead of length(x) because length(x) takes the larger of the dimension of x. Ex: if x = 12x1 matrix, length (x) = 12. if x = 12x100 matrix, length(x) = 100.
for i = 1:size(x, 2)
y{i} = x(:, i);
end
  댓글 수: 6
OCDER
OCDER 2017년 9월 21일
You're welcome!
Stephen23
Stephen23 2017년 9월 22일
"maybe i dont need to split the matrix"
Splitting the matrix will almost certainly be the wrong approach. Data should always be kept together as much as possible, because this actually makes processing it much easier. You can easily perform all sorts of numeric and statistical calculations on one array of data, but this will become a nightmare to do on lots of separate little arrays, even if they are split into a cell array.
Guillaume's answer shows a much better approach.

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

추가 답변 (1개)

Guillaume
Guillaume 2017년 9월 22일
"The values in my matrix is the monthly developement of the stock market. My final result is a table displayed in the command window which shows the average developement for each month/year and it shall also mark the best and worst developement".
In which case, splitting the matrix is the wrong way to approach this:
%build demo table
date = datetime + (0:-4:-800)';
stock = randi([0 100], size(date));
dailystock = table(date, stock); %note that table is ordered here. It doesn't even have to be
%compute monthly average min and max:
[bin, bindates] = discretize(dailystock.date, 'month');
[daterow, ~, group] = unique(bin);
months = bindates(daterow)';
months.Format = 'MM/yyyy';
monthlystock = table(months, ...
splitapply(@mean, dailystock.stock, group), ...
splitapply(@max, dailystock.stock, group), ...
splitapply(@min, dailystock.stock, group), ...
'VariableNames', {'month', 'monthly_mean', 'monthly_max', 'monthly_min'})

카테고리

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