return many value in function.

1 view (last 30 days)
Khairul Nur
Khairul Nur on 5 Nov 2019
Edited: Steven Lord on 5 Nov 2019
hi, i have this main function that called calc_centroid(data) as folow:
[cc1,cc2,cc3,cc4] = calc_centroid(data) %is it correct syntax to accept the return value?
Meanwhile in the function define as follow:
function [c1,c2,c3,c5] = calc_centroid(data)
k1_1= sum(data{1:10,{'Var1'}})/10
k1_2= sum(data{1:10,{'Var2'}})/10
k1_3= sum(data{1:10,{'Var3'}})/10
k1_4= sum(data{1:10,{'Var4'}})/10
k1_5= sum(data{1:10,{'Var5'}})/10
k1_6= sum(data{1:10,{'Var6'}})/10
k1_7= sum(data{1:10,{'Var7'}})/10
k1_8= sum(data{1:10,{'Var8'}})/10
k1_9= sum(data{1:10,{'Var9'}})/10
k1_10= sum(data{1:10,{'Var10'}})/10
k2_1= sum(data{11:20,{'Var1'}})/10
k2_2= sum(data{11:20,{'Var2'}})/10
k2_3= sum(data{11:20,{'Var3'}})/10
k2_4= sum(data{11:20,{'Var4'}})/10
k2_5= sum(data{11:20,{'Var5'}})/10
k2_6= sum(data{11:20,{'Var6'}})/10
k2_7= sum(data{11:20,{'Var7'}})/10
k2_8= sum(data{11:20,{'Var8'}})/10
k2_9= sum(data{11:20,{'Var9'}})/10
k2_10= sum(data{11:20,{'Var10'}})/10
k3_1= sum(data{21:30,{'Var1'}})/10
k3_2= sum(data{21:30,{'Var2'}})/10
k3_3= sum(data{21:30,{'Var3'}})/10
k3_4= sum(data{21:30,{'Var4'}})/10
k3_5= sum(data{21:30,{'Var5'}})/10
k3_6= sum(data{21:30,{'Var6'}})/10
k3_7= sum(data{21:30,{'Var7'}})/10
k3_8= sum(data{21:30,{'Var8'}})/10
k3_9= sum(data{21:30,{'Var9'}})/10
k3_10= sum(data{21:30,{'Var10'}})/10
k4_1= sum(data{31:40,{'Var1'}})/10
k4_2= sum(data{31:40,{'Var2'}})/10
k4_3= sum(data{31:40,{'Var3'}})/10
k4_4= sum(data{31:40,{'Var4'}})/10
k4_5= sum(data{31:40,{'Var5'}})/10
k4_6= sum(data{31:40,{'Var6'}})/10
k4_7= sum(data{31:40,{'Var7'}})/10
k4_8= sum(data{31:40,{'Var8'}})/10
k4_9= sum(data{31:40,{'Var9'}})/10
k4_10= sum(data{31:40,{'Var10'}})/10
c1 = [k1_1 k1_2 k1_3 k1_4 k1_5 k1_6 k1_7 k1_8 k1_9 k1_10]
c2 = [k2_1 k2_2 k2_3 k2_4 k2_5 k2_6 k2_7 k2_8 k2_9 k2_10]
c3 = [k3_1 k3_2 k3_3 k3_4 k3_5 k1_6 k3_7 k3_8 k3_9 k3_10]
c4 = [k4_1 k4_2 k4_3 k4_4 k4_5 k4_6 k4_7 k4_8 k4_9 k4_10]
end
however, there is error said :
Output argument "c5" (and maybe others) not assigned during call to "calc_centroid".
Error in mainKmeans (line 26)
[cc1,cc2,cc3,cc4] = calc_centroid(data)
emm..i never use c5, howcome the error stated c5? and do i use the return value correcly. BTW iam not sure how to solve this error. TQIA for your attention.Please do help me :(

Accepted Answer

ME
ME on 5 Nov 2019
You do have c5 in your function line. It currently says:
function [c1,c2,c3,c5] = calc_centroid(data)
Perhaps, just swapping that for a c4 will do the trick?
  1 Comment
Khairul Nur
Khairul Nur on 5 Nov 2019
mybad, do not notice that..its working after changing to c4

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 5 Nov 2019
Edited: Steven Lord on 5 Nov 2019
k1_1= sum(data{1:10,{'Var1'}})/10
k1_2= sum(data{1:10,{'Var2'}})/10
k1_3= sum(data{1:10,{'Var3'}})/10
k1_4= sum(data{1:10,{'Var4'}})/10
Numbered variables like this are a code smell.
Based on this syntax I believe your data variable is a table or timetable. If that's the case extract all the variables from the table into an array (if they can be concatenated together); use indexing with a vector of table variable names, variable indices, or vartype; or use varfun.
data = array2table(reshape(1:100, [10 10]));
% First approach
k_first = sum(data.Variables, 1)
% Second approach
varNames = {'Var1', 'Var2', 'Var3', 'Var4', 'Var5', ...
'Var6', 'Var7', 'Var8', 'Var9', 'Var10'};
k_second_names = sum(data{:, varNames}, 1)
k_second_numeric = sum(data{:, 1:10}, 1)
k_second_vartype = sum(data{:, vartype('numeric')}, 1)
% Third approach
k_third = varfun(@sum, data, 'OutputFormat', 'uniform')

Tags

Community Treasure Hunt

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

Start Hunting!