You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Divide matrix in subgroups
2 views (last 30 days)
Show older comments
Hello everyone, just a simple question : having a matrix X ,how can I divide the total number of columns in 24 subgroups and each subgroup give a number as variable name? Thanks.
Accepted Answer
Star Strider
on 15 Apr 2021
Note that assigning each matrix to a different variable is not advisable. Keep them as the cell array, and refer to them as elements of it, using subscripts.
Dividing a (9x9) matrix into 9 (3x3) matrices would work as:
A = randi(9, 9);
Adiv = mat2cell(A, [1 1 1]*3, [1 1 1]*3);
.
14 Comments
Fercho_Sala
on 15 Apr 2021
@Star Strider it works good because you know the number of rows and columns, but for example; in the case you don't know the number of rows, and you just want to divide the number of rows according to a vector time, in this case 24 hours. How could you do it?
Star Strider
on 15 Apr 2021
I would need more information as to what your matrix contains, and how you want to divide it.
It is necessary to know all the dimensions of the matrix in order to use the mat2cell function.
Fercho_Sala
on 15 Apr 2021
Ok, I have a matrix (R) of size (249X1451), I need to divide the number of columns in 24. The matter is that normally I don’t know the number of columns, sometimes are more or sometimes are less this is aleatory, but always the number of columns fits into 24h because is the data for one day.
Here is an example of the way I cut the data, I know that for this especific case (:,1:151.1457) it means the first 2.5 hours(00:00 - 2:30). But sometimes that number of columns are no the same (1451) it changes and I have to recalculate the process when the matrix has another different number of columns. Thank you
P.S the number of rows is always equal.
smoothdata(((R(:,1:151.1457))),'movmedian',3,'omitnan');
Star Strider
on 15 Apr 2021
I have no idea what is in the matrix, or how the columns are designated.
If the first row are time values, it might be possible to use findgroups or unique to label the columns by the hour value, and then mat2cell could divide them appropriately given the number of common hours in each group. That would require a bit of extra code, however would likely not be a significant problem.
If there is no such designation, then it would likely not be possible to divide the columns correctly.
Fercho_Sala
on 15 Apr 2021
Star Strider
on 15 Apr 2021
Edited: Star Strider
on 16 Apr 2021
This turned out to be much more straightforward than I anticipated:
L1 = load('Fercho_Sala R.mat');
L2 = load('Fercho_Sala t.mat');
L3 = load('Fercho_Sala y.mat'); % Not Required For This
R = L1.A;
t = L2.A;
t = datevec(t);
[Ut,ia,idx] = unique(t(:,4)); % Unique Hours
Counts = accumarray(idx, 1); % Items In Each Hour
C = mat2cell(R, size(R,1), Counts); % Cell Array Of Partitioned ‘R’ Matrix
producing:
C =
1×24 cell array
Columns 1 through 5
{249×60 double} {249×57 double} {249×60 double} {249×61 double} {249×60 double}
...
Columns 21 through 24
{249×61 double} {249×61 double} {249×60 double} {249×61 double}
This was easier to do with datevec and accumarray than with findgroups and splitapply, at least for me.
Fercho_Sala
on 16 Apr 2021
@Star Strider thank you so much for your help, this is the right answer !! :)
Star Strider
on 16 Apr 2021
As always, my pleasure!
Fercho_Sala
on 19 Apr 2021
@Star Strider how can I modify the script in the case I'd like to use this for every 3 or 6 hours?
Star Strider
on 19 Apr 2021
Interesting problem!
This works:
L1 = load('R.mat');
L2 = load('t.mat');
L3 = load('y.mat');
R = L1.A;
t = L2.A;
t = datevec(t);
[Ut,ia,idx] = unique(t(:,4)); % Unique Hours
Counts = accumarray(idx, 1); % Items In Each Hour
HoursGroup = 3; % Number Of Hours In Each Group
Countsmtx = reshape(Counts, HoursGroup, []); % Reshape ‘Counts’ Vector To Matrix
Counts = sum(Countsmtx); % Sum Columns To Get New ‘Groups’ Vector
C = mat2cell(R, size(R,1), Counts); % Cell Array Of Partitioned ‘R’ Matrix By ‘Groups’ Vector
It works by using reshape to create ‘Countsmtx’, and then sums the columns of the matrix to create a new ‘Counts’ vector.
Choose how you want to divide them with the ‘grouping’ variable ‘HoursGroup’ (here 3) that must be an integral divisor of 24 , so (1, 2, 3, 4, 6, 8, 12, 24). The code will fail for any other values.
Fercho_Sala
on 21 Apr 2021
@Star Strider that's the issue,, because normaly for this case it works good with an integral divisor of 24, however sometimes I just have to do it for every 30 minutes; in this case it will be a (0.5).
for the 30minutes option the easiest way is the one you provided before and I was wondering how I can divide each colum of the "C" (1X24) array, by the middle? Saying that this is just 30 minutes. Thanks.
Star Strider
on 21 Apr 2021
Try this:
L1 = load('R.mat');
L2 = load('t.mat');
L3 = load('y.mat');
R = L1.A;
t = L2.A;
tdn = datevec(t); % Date Vectors
MinutesGroup = 30; % Number Of Minutes In Each Group
group = rem(tdn(:,5), MinutesGroup)==0; % Grouping Variable ‘Steps’
[Ut,ia,idx] = unique(cumsum(+group)); % Unique Groups
Counts = accumarray(idx, 1); % Items In Each Group
C = mat2cell(R, size(R,1), Counts); % Cell Array Of Partitioned ‘R’ Matrix By ‘Groups’ Vector
It took a few minutes to figure out how to do this, and make it as robust as possible. This should allow any integer value of ‘MinutesGroup’ between 1 and numel(t) to define the partitions. (I checked it a number of different values for ‘MinutesGroup’ using the time vector as datevec rows, and it partitions everything correctly, at least with the data set provided.)
It might be possible to do a similar analysis with retime, however you requested a way to partition the data, not analyse it, and this code does exactly that.
Fercho_Sala
on 22 Apr 2021
@Star Strider yes, analysis of data is another different thing, thanks a lot for your knowledge. :)
Star Strider
on 22 Apr 2021
As always, my pleasure!
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)