필터 지우기
필터 지우기

Why am I getting "Too many output arguments"?

조회 수: 5 (최근 30일)
gm76
gm76 2013년 3월 10일
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!

답변 (1개)

Cedric
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).

카테고리

Help CenterFile Exchange에서 MATLAB Mobile Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by