# print the element of a matrix

201 views (last 30 days)
mohammed almakaleh on 17 May 2020
Edited: mohammed almakaleh on 17 May 2020
i wrote this code to print the largest element in the first row of a matrix, but it print the index of the largest number?
how cam i print largest element??
matrix=input("Enter the Matrix");
max=0;
[r,c]=size(matrix);
d=c[j];
n=length(matrix);
for i=0:1
for j=0:c
if [j]> max
max=[j];
end
end
end
disp(max);

Stijn Haenen on 17 May 2020
You can use:
a=max(matrix(1,:));
disp(a)
to disp the max value from the first row,
or
b=max(matrix(:,1));
disp(b)
to get the max value from the first column
you can use
[a,x]=max(matrix(1,:));
to get the value and the positions of the max element.
mohammed almakaleh on 17 May 2020
thanj you for hlep
is the any way to print it with out using (max) function?

Stijn Haenen on 17 May 2020
a=-Inf;
for i=1:numel(matrix(:,1))
if matrix(i,1)>a;
a=matrix(i,1);
end
end
disp(a)
mohammed almakaleh on 17 May 2020
Edited: mohammed almakaleh on 17 May 2020
thanx a lot