How to pass a cell to a function?
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a cell array of size (1344X1). each cell element contains a matrix: first row: 22x40 double, second row 22X52 double,, third row 22X112 double, and so on (My data points are all 22-dimentional). Now I want to pass all elements of the cell array at the same time to a function. I tried the following, but it seems to pass the cell emoluments one by one.
[centers, membership_idx] = myKmeans(data(:,1));
1 个评论
Guillaume
2014-12-21
It's not very clear what you want to do. All the elements of the cell array is basically the cell array itself, so why can't you pass the cell array?
What does your function take as inputs?
回答(1 个)
Image Analyst
2014-12-21
编辑:Image Analyst
2014-12-21
You do not have any 22 dimensional arrays. Your cell array is a 1-D array. Inside each cell is a 2-D array with 22 rows and some variable number of columns. You might find the FAQ useful.
Since your cell array has only 1 column and 1344 rows, data(:,1) would be the first column, but since it's a column vector it would pass all cells in your cell array, not just one cell. So what you said happens does not agree with what you described. It should be passing the whole cell array but that's not the way it's usually done. Usually to pass a whole array, you don't attach parentheses and indexes to it. You just use the name of the cell array all by itself.
Let's say your cell array is called "data" and you want to pass the entire cell array into some custom function you wrote called "myKmeans()". To do that you'd do:
[centers, membership_idx] = myKmeans(data);
3 个评论
Image Analyst
2014-12-21
You can pass the cell array to the function. The problem came inside the function when you tried to sum something. Evidently the sum() function does not like cell arrays and you need to extract the matrix with {} or use cell2mat().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!