finding columns and rows with largest sum of squares

Hello there, I am a beginner with MATLAB and I am trying to solve this problem. (I need to write a FMF function file)
Having a matrix A i need to find the column and the row which has the largest sum of squares, once i find that, i need to substitute the row and the column as 1st row and column of a matrix B. I am given as outputs [nrows ncols imax jmax B] and as inputs only A.
that's what i came up with
function [nrows ncols imax jmax B] = ...title of script
% function to find max sum of squares row and column
% and to put that into a matrix B as rise row and col
A=rand(3,3); % i put dummy values but how can i be more general?
[nrows,ncols]=size(A); %size of the matrix
nrows=3; ncols=3
imax=0; jmax=0, B=[] % initialisation of values
for j=1:ncols %loop over number of columns
jmax= max([1,j].^2+[2,j].^2+[3,j].^2) % find col with largest sum of squares
end
for i=nrows:1; %loop over number of rows
imax=max([i,1].^2+[i,2].^2+[i,3].^2) %find row with largest sum of squares
end
B=rand(3,3);
B(1,:)=jmax %substitute 1st col of B with jmax
B(:,1)=imax %substitute 1st row of B with imax
end
my doubts are: 1)how can i randomize the size of the matrix (since it's not given) and still be able to identify which row/col has the greatest sum of squares? 2) is my method to find the largest sum of squares correct?
Thanks for your Help!!!

Answers (1)

Some hints:
A=magic(3)
B=A.^2
C=sum(B)
[Cmax,IndC]=max(C)
D=sum(B,2)
[Dmax,IndD]=max(D)

Asked:

on 20 Oct 2011

Community Treasure Hunt

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

Start Hunting!