Divide matrix in to vectors using for-loop

13 visualizaciones (últimos 30 días)
Olle Haglund
Olle Haglund el 21 de Sept. de 2017
Comentada: Stephen23 el 22 de Sept. de 2017
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 comentario
Guillaume
Guillaume el 21 de Sept. de 2017
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.

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 21 de Sept. de 2017
Editada: OCDER el 21 de Sept. de 2017
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 comentarios
OCDER
OCDER el 21 de Sept. de 2017
You're welcome!
Stephen23
Stephen23 el 22 de Sept. de 2017
"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.

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 22 de Sept. de 2017
"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'})

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by