Using mat2cell in a proper way
Show older comments
I have a matrix with format 4822x35 doubles and 2418x35 doubles, is it possible to create cells with 10x35 cells? If yes, what should I write?
If I refer to the MATLAB documentation, the thing that I should write will gonna be like this (according to my reasoning)
>> mat2cell(A,[241.8 241.8 241.8 241.8 241.8 241.8 241.8 241.8 241.8 241.8],[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1])
this example is for 2418x35 doubles. But when I run the code, the error said that the array 1 must be positive integers. So how should I do the splitting?
Answers (1)
chicken vector
on 21 Apr 2023
Edited: chicken vector
on 21 Apr 2023
1 vote
When you use mat2cell, besides the first entry that is the matrix you want to modify into a cell, the other inputs give the instruction on how to divide the matrix into smaller chunks.
You can imagine a matrix as a square of non-divisible bricks.
What you are trying to do is to create a 241.8x1 matrix, meaning that you are "cutting" some of this bricks.
In other words, the dimension of a matrix must be positive integers and dividing a 2418x1 matrix into 10 subarrays is simply not possible.
You have to come up with different solutions for your problem or try post your code to seek for specific help.
Maybe interp1 might be helpful to resize the original matrix to yoru needs.
In general you want to use interp2, but you don't have any problem along the columns.
5 Comments
Edoardo
on 21 Apr 2023
chicken vector
on 21 Apr 2023
If you want further help you should post some code and/or data and explain precisely what you need (TUTORIAL).
In other words, the dimension of a matrix must be positive integers and dividing a 2418x1 matrix into 10 subarrays is simply not possible.
Dividing it into 10 subarrays of the same size is not possible. You could divide it into unequally sized subarrays just fine.
sz = 2418;
n = 10;
blocksize = floor(sz/n);
sizes = [repmat(blocksize, 1, n-1), sz-(n-1)*blocksize]
Let's check that our blocks of length given by the elements in sizes use up all the elements.
[sum(sizes), sz]
To use sizes:
x = 1:sz;
C = mat2cell(x, 1, sizes);
We can check that the cells in C contain vectors of the correct length:
actualSizes = cellfun(@numel, C)
"I need to divide the data into 10 minutes"
I doubt that splitting up your data would be a good approach. Then you cannot use inbuilt tools effectively:
So far you have not told us the most important information: how do the rows of your data correspond to minutes? Were they sampled with a constant sample rate, or do you have some timestamp data?
"Of course if-else commands are also needed in this case,"
I doubt that.
Note that by assuming that you need to split data up into cell arrays and use IFs you are making this mistake:
Your task is not to split data up into a cell array. Your task is to determine when specified events occur in your data.
You should have asked about that in the first place.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!