Hi, I have the attached data array as an example, I need to split it into 3 equal chunks! Then I need to apply that for any other array with the same type but with different length.

2 件のコメント

Adam Danz
Adam Danz 2018 年 9 月 24 日
There's one variable stored in your mat file so it would be a lot simpler to help you if you just describe that variable rather than require multiple people do download a mat file, load it in matlab, etc.
The variable Cb is a column vector of length 4897 which is not divisible by 3 so it is impossible to divide it into 3 chunks of equal length.
sana3 sal
sana3 sal 2018 年 9 月 24 日
So, suppose it is any other array that has divisible length!?

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2018 年 9 月 24 日

1 投票

Try this:
s = load('Cb.mat');
Cb = s.Cb;
var3c = @(oldvar) mat2cell(oldvar(:), [fix(numel(oldvar)/3) *[1, 1], numel(oldvar)-2*fix(numel(oldvar)/3)], 1); % Create New Matrix From Original Vector
newCb = var3c(Cb)
newCb =
3×1 cell array
{1632×1 double}
{1632×1 double}
{1633×1 double}
You cannot divide your vector equally, although you can get close. The ‘var3c’ anonymous function works with any length vector. It returns a cell array of 3 column vectors, even if the original vector is a row vector.

5 件のコメント

sana3 sal
sana3 sal 2018 年 9 月 24 日
It is okay, there is no problem in dividing the vector into {1632×1 double} {1632×1 double} {1633×1 double} It worked with me thanks. But now , how i can access each cell alone? for example,i need to find the standard deviation for each part?
Star Strider
Star Strider 2018 年 9 月 24 日
My pleasure.
Use the cellfun (link) function:
newCbstd = cellfun(@std, newCb)
newCbstd =
0.521806807562178
0.061049922231889
0.096887917396344
sana3 sal
sana3 sal 2018 年 9 月 24 日
I used this, is it fine?
S1=std(newCb{1,:})
S2=std(newCb{2,:})
S3=std(newCb{3,:})
sana3 sal
sana3 sal 2018 年 9 月 24 日
Any way, thank you alot you saved my life ♥
Star Strider
Star Strider 2018 年 9 月 24 日
As always, my pleasure!
If it gives you the same result as I got, that would work.

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2018 年 9 月 24 日
編集済み: Adam Danz 2018 年 9 月 24 日

3 投票

This will take a column vector whose length is divisible by 3 and reshape it into a matrix with 3 columns of equal length. Each column of the new matrix will be one of your chunks.
Cb = (1:300)';
n = 3;
Cbmat = reshape(Cb, [], n);
Cbmat(:,1) is chunk 1, Cbmat(:,2) is chunk 2, etc.
This will break if Cb is not divisible by n.

カテゴリ

ヘルプ センター および File Exchange で Multidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by