关于MATLAB的问题 请大家帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/05/22 15:58:17
建立5x6矩阵,要求输出矩阵的第n行元素,值超过矩阵的行数时 自动转为输出矩阵最后一行 并给出出错信息。
用MATLAB程序设计
谢谢了!
恩谢谢了,能用try函数或者switch函数吗?

a=rand(5,6);
n=input('input the row : ');
if (n<5)
a(n,:)=[];
else
a(5,:)=[];
end

function[out] = search_matrix(a,i)
index =length(a);
if i>=index
error('Out of range');
out=a(index,:);
else
out=a(i,:)
end
end

function[out] = search_matrix(a,i)
try
out=a(i,:)
catch
out=a(end,:)
end
end