How to simplify this code without having multilevel indexing error???

1 回表示 (過去 30 日間)
hana razak
hana razak 2017 年 10 月 5 日
コメント済み: hana razak 2017 年 10 月 11 日
Hi,
I've a matData consists of (1x96 struct) and each structure, ji has (1x35 cell) and for each cell has (20x2 double). I want to arrange the data into (1400x96 double).
As for now, I only managed to get (1400x1 double) separately for each matData by using the code below,
a1 = matData(1).ji;
aa1 = cell2mat (a1);
p1 = reshape(aa1, 1400, 1);
a2 = matData(2).ji;
aa2 = cell2mat (a2);
p2 = reshape(aa2, 1400, 1);
a3 = matData(3).ji;
aa3 = cell2mat (a3);
p3 = reshape(aa3, 1400, 1);
What should I do to simplify this code without having manually typing for all 96 matData?
I can't straightly write as,
aa = matData(1:35).ji{1:35};
because I keep getting this error 'Field reference for multiple structure elements that is followed by more reference blocks is an error'.
Thank you

採用された回答

Cedric
Cedric 2017 年 10 月 5 日
編集済み: Cedric 2017 年 10 月 5 日
Here is one way. I build a simple test data set with 4 structs, each ji field contains 3 cells, and each cell contains a 2x2 numeric array:
S(1).ji = {[10 12;11 13],[14 16;15 17],[18 20;19 21]} ;
for k = 2:4, S(k).ji = cellfun( @(x)k*x, S(1).ji, 'UniformOutput', false ) ; end
To aggregate:
>> bigArray = cell2mat( cellfun( @(x)x(:), vertcat( S.ji ).', 'UniformOutput', false ))
bigArray =
10 20 30 40
11 22 33 44
12 24 36 48
13 26 39 52
14 28 42 56
15 30 45 60
16 32 48 64
17 34 51 68
18 36 54 72
19 38 57 76
20 40 60 80
21 42 63 84
Evaluate the internal expressions to understand, and ask if anything is unclear. Looking at your code, the only thing that you may not be familiar with is S.ji (with no struct index), which is a comma separated list (CSL), that we concatenate with VERTCAT. Then all the rest is about manipulating cell and numeric arrays.
  14 件のコメント
Cedric
Cedric 2017 年 10 月 11 日
編集済み: Cedric 2017 年 10 月 11 日
If your data is not too confidential, the simplest way to go would be to drop me an email using my personal email (you get it when I send you notices that I posted a comment to your thread), attaching a MAT-File with matData.
If you cannot do this, the whole point of the test that I suggested was to identify a situation where there is a size mismatch.
See, if you have
>> A = {randi(10,2,3),randi(10,2,3);randi(10,2,3),randi(10,2,2)}
A =
2×2 cell array
{2×3 double} {2×3 double}
{2×3 double} {2×2 double}
where there is a size mismatch (A{2,2} is 2x2 and not 2x3 like the others), you will be able to concatenate all contents horizontally but not vertically:
>> horzcat( A{:} )
ans =
7 7 10 3 3 6 4 5 8 5 7
10 7 6 1 9 5 10 7 7 8 7
>> vertcat( A{:} )
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
The horizontal concatenation works technically, but it doesn't mean that it is really the operation that you intended to do. It certainly let the size mismatch undetected.
My guess was that you ran my code (that uses a vertical concatenation), got this type of error, and then tried using a horizontal concatenation. And while it went through, the size doesn't match your expectation.
Your comment seems to indicate that you found the size discrepancy. Now all you have to do is to understand whether it should be concatenated with the rest (maybe it is just invalid), and in what direction.
hana razak
hana razak 2017 年 10 月 11 日
Yes, my data is not too confidential. You can send the notice in order for me to attach the MAT-File. Thank you very much. This would be a great help for me.

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

その他の回答 (1 件)

KL
KL 2017 年 10 月 5 日
s2c = struct2cell(matData);
c2c = cellfun(@(x) x',reshape(s2c,[],1,1),'uni',0);
c2a = cell2mat(cellfun(@cell2mat,c2c,'uni',0));
  2 件のコメント
hana razak
hana razak 2017 年 10 月 6 日
編集済み: hana razak 2017 年 10 月 7 日
It only got half way through. I've got an error for this code;
c2a = cell2mat(cellfun(@cell2mat,c2c,'uni',0));
It just stated as below,
'Error in test2 (line 19) c2a = cell2mat(cellfun(@cell2mat,c2c,'uni',0));'
I don't have any idea how to correct it. So, is there any other way to get it right? Thanks a lot
hana razak
hana razak 2017 年 10 月 11 日
編集済み: hana razak 2017 年 10 月 11 日
I got it..
The codes are work very well. The error was due to bulk data.
Thank you so much. It was really very helpful!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by