如何在Matlab中将矩阵向量传递给函数参数 [英] How can I pass a matrix vector into a function argument in matlab
本文介绍了如何在Matlab中将矩阵向量传递给函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个10 x 3的矩阵,并希望将10行中的每行用作该函数的参数,该函数希望使用从1到10的迭代获得3个参数. 问题是我无法将每个行向量直接传递给需要3个参数的函数.如何将矩阵行转换为函数可接受的格式?
I have a 10 x 3 Matrix and would like to use each of the 10 rows as arguments to a function expecting 3 arguments using an iteration from 1 to 10. Problem is that I cannot pass each row vector directly into the function expecting 3 arguments. How can I convert the matrix rows to a format acceptable by my function?
这是函数:
XXX = obj(Kc, T1, T2);
呼叫代码:
for i = 1:100
pop(i,1) = 50 - rand*(50-1);
pop(i,2) = 1 - rand*(1-0.1);
pop(i,3) = 0.2 - rand*(0.2-0.01);
Kc(i) = pop(i,1);
T1(i) = pop(i,2);
T2(i) = pop(i,3);
end
for j = 1:10
kk = randperm(100);
Tour1 = pop(kk(1:10),:);
ZET(j) = obj(Tour1(j,:));
end
Tour1是10 x 3矩阵,其行需要变为Kc,T1,T2. 谢谢.
Tour1 is the 10 x 3 matrix whose rows need to become Kc, T1, T2. Thanks.
推荐答案
转换为单元格数组:
for j = 1:10
kk = randperm(100);
Tour1 = pop(kk(1:10),:);
temp = mat2cell(Tour1(j,:),1,ones(1,numel(Tour1(j,:))))
ZET(j) = obj(temp{:});
end
这篇关于如何在Matlab中将矩阵向量传递给函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文