Too many input arguments error when cell2mat and mean functions are used

I have five cluster data saved in my cell array iniclu. Each row of cell array contains varying number of columns such as 23 27 26 17 7 so that the shortage of columns are marked as [] to the max of 27. Now i want to process cell array row one by one to find mean or centroid of each cluster. My usage is shown in code, but it throws error.
count = [23 27 26 17 7]
iniclu = cell(1,1); %later i built the cluster data as shown in fig
for i = 1:5
bb = [];
bb = cell2mat(iniclu{i,1:count(i)});
centroids(i,:) = mean(bb,1);
end
But it throws too many input arguments error, hence i could not get expected op as centroids having 5x2 means data. I have attached content of iniclu data. Pls clarify!

3 Commenti

The cell2mat function only need one cell array input argument. I think the code should be:
bb=cell2mat(iniclu(i,:));
Thank you. But your answer partially solves the problem when i try to calculate mean. Can't i get cell2mat output as ,for ex first cell row, 23x2 size instead of 1x46 double?
no, you have different sizes of cells; you can only put them together as a vector (unless you are willing to pad the entries)

Accedi per commentare.

 Risposta accettata

V = [23;27;26;17;7];
N = numel(V);
C = nan(N,2);
for k = 1:N
M = vertcat(iniclu{k,1:V(k)}); % comma-separated list
C(k,:) = mean(M,1);
end

Più risposte (1)

bb = cell2mat(iniclu(i,1:count(i)));

Use a ( ) in place of { }

9 Commenti

If you use { } the result is not a cell, so you don't need cell2mat function to convert

Thank you. But I could not successfully calculate mean. i think my iniclu data structure of x y coordinates for the five clusters is not consistent with mean function of matlab.
@NALLARASU KRISH, you can get the cell2mat output as 23 x 2 if you reshape
bb = cell2mat(iniclu(i,1:count(i)));
bb = reshape(bb,[],2);
centroids{i} = mean(bb,1); % The 5th row unequal data size compared to remaining rows
Your idea reshaped ...but with a trick in it... ha ha ha! Anyway i learnt abt reshape, thank you. Reshape took each 2 values from row and placed it in same column, ie reshape fills up column-wise. Hence pair of x coor and y coor appeared in same column instead of at col 1 and col 2, thus leading to false mean value or centroid.
@NALLARASU KRISH, ok, try the below
bb = cell2mat(iniclu(i,1:count(i)));
bb = reshape(bb,2,[]).';
centroids{i} = mean(bb,1); % The 5th row unequal data size compared to remaining rows
Still not yielded. let me show what happens for each style of statement. Assume 5 pairs
60 52
69 44
85 19
5 99
56 68
it is in iniclu as [60 52] [69 44] [85 19] [5 99] [56 68]
bb = cell2mat(iniclu(1,1:5));
o/p
60 52 69 44 85 19 5 99 56 68
bb = reshape(bb,[],2);
o/p
60 19
52 5
69 99
44 56
85 68
bb = reshape(bb,[],2).';
o/p
60 52 69 44 85
19 5 99 56 68
It looks like small problem, but demands more time!
@NALLARASU KRISH you have used the below line incorrectly, it should be as shown below
bb = reshape(bb,2,[]).';
OMG! This is working, you are really great. Thank you!! I accept this answer too. Sorry for my carelessness. Could you explain the difference bw bb = reshape(bb,2,[]).'; and bb = reshape(bb,[],2).'; that did the magic, @VBBV ?
Look at the sizes of the outputs and the arrangement of data in those outputs.
bb = 1:10
bb = 1×10
1 2 3 4 5 6 7 8 9 10
bb1 = reshape(bb, 2, []).'
bb1 = 5×2
1 2 3 4 5 6 7 8 9 10
bb2 = reshape(bb, [], 2).'
bb2 = 2×5
1 2 3 4 5 6 7 8 9 10

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by