could anyone help me how to solve the error in the following code

I am having cell array
B=10x1 cell
which contains
10x4 double
10x4 double
10x4 double
10x4 double
10x4 double
10x4 double
10x4 double
10x4 double
10x4 double
10x4 double
I used the following code to convert it
A=cell(size(B));
for i=1:numel(B)
A{i}=splitapply(@(x){x}, 1:numel(B{i}) ,B{i});
disp("YTest{" +i+ "}:"), B{i}
for j=1:numel(A{i})
disp("A{" +i+ "}{"+j+"}:"), A{i}{j}
end
disp ' '
end
But it results in error. Could anyone please help me to overcome it.

2 件のコメント

jessupj
jessupj 2021 年 7 月 16 日
what is the error, exactly?
what is the line
splitapply( @(x) {x}, 1:numel(B{i}) ,B{i} )
supposed to do? it looks like that's where is problem to me
jaah navi
jaah navi 2021 年 7 月 16 日
Error using splitapply
Group numbers must be a vector of positive integers, and cannot be a sparse vector.
Error in line 3
A{i}=splitapply(@(x){x}, 1:numel(B{i}) ,B{i});

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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 7 月 16 日

0 投票

The last parameter to splitapply must be a vector of positive integers, one for each row of input, with the number indicating which group the row belongs to.
It is not clear that your B values are integers. It is clear that B{i} is not a vector.
Perhaps you accidentally exchanged the second and third parameters.

8 件のコメント

jaah navi
jaah navi 2021 年 7 月 16 日
The code executes
A=cell(size(B));
for i=1:numel(B)
A{i}=splitapply(@(x){x}, 1:numel(B{i}) ,B{i});
disp("YTest{" +i+ "}:"), B{i}
for j=1:numel(A{i})
disp("A{" +i+ "}{"+j+"}:"), A{i}{j}
end
disp ' '
end
when B is
1x4 double
Now I want to modify the code as now B is
10x4 double
Could you please help me how to execute the code for
B=10x4 double.
Walter Roberson
Walter Roberson 2021 年 7 月 16 日
That is not possible. The last parameter to splitapply must be a vector. Never a 2d array.
jaah navi
jaah navi 2021 年 7 月 16 日
Ok. Is there any other way that can convert 10X 4 double into 10X4 cell.
Walter Roberson
Walter Roberson 2021 年 7 月 16 日
num2cell()
jaah navi
jaah navi 2021 年 7 月 16 日
if i use num2cell it executes, but the result is different from what i need.
jaah navi
jaah navi 2021 年 7 月 16 日
I think i need to use splitapply but I am not aware how to use it in my case.
Could you please help me on this.
Unfortunately, you did not explain what output you want.
rng(12345)
B{1} = randi(9, 3, 4);
B{2} = randi(9, 3, 4);
celldisp(B)
B{1} = 9 2 9 6 3 6 6 7 2 6 7 9 B{2} = 1 6 9 7 1 8 7 5 3 8 6 3
What exact output would you want for the above?
jaah navi
jaah navi 2021 年 7 月 17 日
I want B to convert to A.
B=3x1 cell
2x3 double - [1,1,1 - 1st row
1,2,2]- 2nd row
A=3x1 cell
2x1cell - [1,2,3] 1st row
2x2cell - [1] [2,3] 2nd row

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

カテゴリ

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

タグ

質問済み:

2021 年 7 月 16 日

コメント済み:

2021 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by