Why am I getting "Too many output arguments"?
3 次查看(过去 30 天)
显示 更早的评论
I am trying to run this function with a 3x3 input matrix, and I am getting the "Too many output arguments" error using row_sums.
Here is the code for my main function:
function ms_equal_col_row_diag_sums(square_matrix)
N=length(square_matrix);
magic_sum =(N*(N^2+1))/2;
row_sum=row_sums(square_matrix);
column_sum=column_sums(square_matrix);
ll_ur_diagonol=ll_ur_diagsum(square_matrix);
ul_lr_diagonal=ul_lr_diagsum(square_matrix);
if row_sum(1,:)==column_sum(1,:)==N*magic_sum & ll_ur_diagonol==ul_lr_diagonal==magic_sum
disp(1)
else disp(0)
end
end
This is the code for row_sums:
function row_sums(square_matrix)
sum(square_matrix')
end
row_sums works if I use it individually with the same input. Ex. row_sums([2 7 6; 9 5 1; 4 3 8])=[15 15 15]
Thanks!
0 个评论
回答(1 个)
Cedric
2013-3-10
编辑:Cedric
2013-3-10
Actually row_sums doesn't work as it outputs nothing. However, you have the impression that it works because it displays the output of sum(square_matrix') as the line doesn't end with a ; . For solving this issue specifically, you need to define an output argument:
function s = row_sums(square_matrix)
s = sum(square_matrix') ;
end
Note that SUM can take an optional argument that defines the dimension (1 by default, but you could specify 2).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Instrument Control Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!