function output = IsMagic(n) A= zeros(n); r=n; c=n; row_index = 1; while row_index <= r col_index=1; while col_index <= c A(row_index,col_index) = randi(1*n.^2); col_index=col_index + 1; end row_index= row_index +1;end matrix=A; columnsum=sum(matrix); disp ('The sum for the columns is:') fprintf ('%d,\n', columnsum) rowsum=sum(matrix,2); disp ('The sum for the rows isL') fprintf ('%d,\n', rowsum) d1=sum(diag(matrix)); d2=sum (diag(flip(A))); disp ('The sum for the diagonals is') fprintf ('%d, %d \n',d1,d2) if numel(unique(columnsum))==1 if numel(unique(rowsum))==1 if numel(unique(d1))==1 if numel (unique(d2))==1 if (columnsum(1)==(rowsum(1))&& (columnsum(1))== (d1(1)) && (columnsum(1))==(d2(1))) output=1; else output=0; end end end end end end
The purpose of this code is to create a function called isMagic. The function takes one input n, creates a square matrix of size n × n using random integers between 1 and n2,and returns the logical value true (1) if the resulting matrix is a magic square and false(0) if it not a magic square.
I have done everything, but for some reason the output is not displaying. What am I doing wrong?
Best Answer