Using mat2cell in a proper way

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
chicken vector on 21 Apr 2023
Edited: chicken vector on 21 Apr 2023
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

Thank you! I appreciate your answer, but maybe using "interp1" will make me to do further research ;( Maybe I'll just leave as it is for now, or just wait for someone to explain further XD
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]
sizes = 1×10
241 241 241 241 241 241 241 241 241 249
Let's check that our blocks of length given by the elements in sizes use up all the elements.
[sum(sizes), sz]
ans = 1×2
2418 2418
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)
actualSizes = 1×10
241 241 241 241 241 241 241 241 241 249
Edoardo
Edoardo on 22 Apr 2023
Edited: Edoardo on 22 Apr 2023
@chicken vector to be honest the thing that I want is complex (in general).
Those 4822x35 doubles and 2418x35 doubles is the transformed EEG result (the transformation that I use is Wavelet Transform sym8). I need to divide those 4822 doubles and 2418 doubles into 10 cells because I need to divide the data into 10 minutes. The "35" comes from 35 different electrodes. Then for each electrode I need to locate in what minute the signal becomes abnormal. For example, in electrode 1, the abnormalities happened in minute 5; in electrode 2 in minute 5 and 7; in electrode 3, in minute 7 and 10, ... etc., up until all those 35 electrodes. Finally I need a thing like a truth table in order to write the results. Of course if-else commands are also needed in this case, but I haven't learnt about it for now.
So that is the reason why I ask about converting doubles to cells first, because I need to divide the values in 4822x35 doubles and 2418x35 doubles into 10 minutes first. Do you think that my approach is correct?
Actually, I can do it manually by plotting the graph and look at each of them one-by-one, but since there are 35 electrodes to be care about, it is exhausting, and I'm afraid that I miss somewhere in the middle
Stephen23
Stephen23 on 22 Apr 2023
Edited: Stephen23 on 22 Apr 2023
"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.

Sign in to comment.

Asked:

on 21 Apr 2023

Edited:

on 22 Apr 2023

Community Treasure Hunt

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

Start Hunting!