矩阵作为Matlab函数的输入和输出 [英] Matrix as input and output of Matlab function
问题描述
例如,我想要一个函数让我删除矩阵中最高值为1的行。因此我写道:
%A是输入矩阵
%pict2假设被输出清除矩阵
函数pict2 = clear_(A)
B = A
n = size (A,1)
for i = 1:n
if(max(B(i,:))== 1)
B(i,:)= []
结束
结束
但是我打电话后:
pict2 = clear_(pict)
Matlab提供:
警告:clear_:列表中的某些元素返回值未定义
警告:从
调用clear_在第5列1 pict2 =
I' m不确定哪些元素是未定义的?
输出参数的变量名必须与您想要的变量匹配回。因此,您需要将第一行更改为以下内容,以便您修改 B
并保存并返回。
< pre $
函数B = clear_(A)
你的算法很担心,它不会工作,因为你在试图循环它时正在修改 B
。相反,你可以用下面的表达式来替换你的整个函数,这个表达式计算每行的最大值,然后确定这个值是否等于 1
,并删除这个行(b,[],2)== 1,:) == [];
For example I want to have a function which let me delete rows of my matrix where the highest value is 1. Thus I wrote:
% A is an input matrix
% pict2 suppose to be output cleared matrix
function pict2 = clear_(A)
B=A
n = size(A,1)
for i=1:n
if(max(B(i,:))==1)
B(i,:)=[]
end
end
But after I call:
pict2=clear_(pict)
the Matlab resposes:
"warning: clear_: some elements in list of return values are undefined warning: called from clear_ at line 5 column 1 pict2 = "
I'm not sure which elements were left undefined?
The variable name of your output argument must match the variable that you want to be returned. So you'll want to change the first line to the following so that your modifications to B
are saved and returned.
function B = clear_(A)
As far as your algorithm is concerned, it's not going to work because you are modifying B
while trying to loop through it. Instead, you can replace your entire function with the following expression which computes the maximum value of each row, then determines if this value is equal to 1
and removes the rows where this is the case.
B(max(B, [], 2) == 1, :) == [];
这篇关于矩阵作为Matlab函数的输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!