I have trouble with cell arrays
古いコメントを表示
I currently have a 1x4 cell array where each of the four elements is a 134x1 array of different data types. The cell array was created from a text file by a textscan call. What I wanted is a 134x4 cell array. How do I get there?
1 件のコメント
José-Luis
2014 年 10 月 9 日
Well, a solution is not to use textscan() but a lower level routine like sscanf() that will allow you to specify formats for all elements.
回答 (2 件)
Adam
2014 年 10 月 9 日
res = C{:};
where C is your cell array would work in this case.
5 件のコメント
David
2014 年 10 月 9 日
Adam
2014 年 10 月 9 日
Ah, sorry, I missed the "different data types" part of the question and read it as you wanting a standard array.
Iain
2014 年 10 月 9 日
res = [C{:}];
Adam
2014 年 10 月 9 日
num2cell([c{:}])
I think is what is needed to get the cell array result.
David
2014 年 10 月 9 日
Andrew Reibold
2014 年 10 月 9 日
編集済み: Andrew Reibold
2014 年 10 月 9 日
Is this what you are looking for?
a = {0 0 0 0};
b = {1 1 1 1};
c = {'1' '2' '3' '4'};
d = {'look,';'this';'has';'characters!'};
MyBigCell = {a{:}; b{:}; c{:}; d{:}} % <-- Try this with your cells
Output:
MyBigCell =
[ 0] [ 0] [ 0] [ 0]
[ 1] [ 1] [ 1] [ 1]
'1' '2' '3' '4'
'look,' 'this' 'has' 'characters!'
10 件のコメント
Andrew Reibold
2014 年 10 月 9 日
編集済み: Andrew Reibold
2014 年 10 月 9 日
Convert Character array to cell using cellstr.
Convert the logical array to cell using num2cell
If there is too many to do by hand, write loops to check class and do such :)
a = {0 0 0 0}
b = {1 1 1 1}
c = ['fast'; 'slow'; 'four'; 'five']
d = true(1,4)
c2 = cellstr(c)
c = c2'
d = num2cell(d)
MyCell = {a{:}; b{:}; c2{:}; d{:}}
MyCell =
[ 0] [ 0] [ 0] [ 0]
[ 1] [ 1] [ 1] [ 1]
'fast' 'slow' 'four' 'five'
[ 1] [ 1] [ 1] [ 1]
EDIT: Just noticed my final answer was transposed from what you wanted haha. Here
MyCell'=
[0] [1] 'fast' [1]
[0] [1] 'slow' [1]
[0] [1] 'four' [1]
[0] [1] 'five' [1]
Andrew Reibold
2014 年 10 月 9 日
編集済み: Andrew Reibold
2014 年 10 月 9 日
The great part about scripting is that you really only have to do it one time no matter how many sets you have.
Good luck!
David
2014 年 10 月 9 日
Andrew Reibold
2014 年 10 月 9 日
編集済み: Andrew Reibold
2014 年 10 月 9 日
Last overnight and not be done?
I just ran a test case using larger arrays than yours (144 rather than 134) 1000 times in 30 seconds.
Is the problem related to a human's ability to read.. or is it really related to the ability to code? ;)
jk jk. I dont know how to help bud, I'm sorry. Thought I had a solution.
David
2014 年 10 月 9 日
Matlab is NOT built to handle tons of data efficiently. If you want to handle large amounts of data then you should use a database.
Processor speed is unlikely to be the bottleneck when handling large datasets. Available memory is more likely to be the culprit.
David
2014 年 10 月 9 日
Well, then you could start by not using textscan() but the faster sscanf().
Even faster would be to use a binary format to store your data instead of text. Of course, IO becomes more complicated then.
And if I'm going to be nitpicking, as much as I love Matlab, it is not the best tool to run things fast, but it's one of the better ones to write code fast.
JIT and whatnot, an overhead is an overhead.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!