matlab 结构数组作为参数,如何在函数中将2维数组声明为参数在MATLAB中?(How can I declare 2-dimentional array as parameter in func...

如何在函数中将2维数组声明为参数在MATLAB中?(How can I declare 2-dimentional array as parameter in function In MATLAB?)

我有一个函数,它获取矩阵并对其进行一些操作。 但我不知道如何声明函数的参数。 这是我的功能代码:

function D=Shirinkage(a)

D(1,:)=a(1,:);%this is X1

for i=2:4

D(i,4)=0;

for j=1:3

D(i,j)=1/2*(a(1,j)+a(i,j));

D(i,4)=D(i,j)^2 + D(i,4); %object function

end

end

end

我尝试a(4,4)而不是(函数的参数),但错误没有出现。 错误:

??? Input argument "a" is undefined.

Error in ==> Shirinkage at 3

D(1,:)=a(1,:);%this is X1

我也想正确宣布D

感谢任何帮助。

编辑:我从脚本文件调用我的函数,这样:我有一个2维数组(矩阵),其大小为:4 * 4,其名称为A.我希望我的函数获取此矩阵并执行操作它和结果可以再次保存在其中。

A=Shirinkage(A)

ex A有这个值:

A=[1,2,3,4;2,3,4,5;5,6,7,8;1,2,3,4]

I have a function that gets a matrix and do some operations on it. but I do not know how to declare function's parameter . this is my function Code:

function D=Shirinkage(a)

D(1,:)=a(1,:);%this is X1

for i=2:4

D(i,4)=0;

for j=1:3

D(i,j)=1/2*(a(1,j)+a(i,j));

D(i,4)=D(i,j)^2 + D(i,4); %object function

end

end

end

I tried a(4,4) instead of a (parameter of the function),but the error does not appear. Error:

??? Input argument "a" is undefined.

Error in ==> Shirinkage at 3

D(1,:)=a(1,:);%this is X1

also I want to declare D correctly.

appreciating any help.

Edited: i call my function from a script file , in this way: I have a 2-dimention array(matrix) its size is : 4*4 and its name is A. i want my function gets this matrix and do the operation on it and the result can be saved again in it.

A=Shirinkage(A)

e.x. A has this values:

A=[1,2,3,4;2,3,4,5;5,6,7,8;1,2,3,4]

原文:https://stackoverflow.com/questions/40052317

更新时间:2020-02-02 02:17

最满意答案

你创建的功能工作正常。 我必须预先分配D的大小,因为它在当前代码的每次迭代中都会有所不同。

function D = Shirinkage(a)

D = zeros(size(a));

D(1,:) = a(1,:); %this is X1

for i = 2:4

D(i,4) = 0;

for j = 1:3

D(i,j) = 0.5*(a(1,j) + a(i,j));

D(i,4) = D(i,4) + D(i,j)^2; %object function

end

end

end

通过使用您使用的相同矩阵从命令窗口调用该函数,它给出了以下输出。

A4HsF.jpg

您发布的错误表明该函数未收到参数a 。 如果您的脚本和函数在同一个MATLAB路径中,这应该可以完美地工作。

The function you created is working fine. The only recommendation I have to pre-allocate the size of D as it varies in each iteration in your current code.

function D = Shirinkage(a)

D = zeros(size(a));

D(1,:) = a(1,:); %this is X1

for i = 2:4

D(i,4) = 0;

for j = 1:3

D(i,j) = 0.5*(a(1,j) + a(i,j));

D(i,4) = D(i,4) + D(i,j)^2; %object function

end

end

end

The function was called from command window by using the same matrix you have used and it gave the following output.

A4HsF.jpg

The error you have posted says that the function hasn't received the argument a. If your script and the function are in the same MATLAB path, this should work perfectly.

2016-10-14

相关问答

通过调用函数 x = problemtester(randgen)

MATLAB将首先评估randgen ,因为这是一个函数(并且可以在没有任何参数的情况下调用)。 那时, HELLO WORLD正在印刷。 然后将该函数调用的返回值传递给problemtester ,它将这一个值保存1000次到arr并返回该值。 要使问题problemtester调用函数 randgen ,你必须提供一个函数句柄 ,它是MATLAB相当于函数指针。 x = problemtester(@randgen)

这打

...

你能用prod(sum(x,2))吗? 我想如果你重新排列你的总和中的条款,你会发现你可以将你的行的总和相乘,你会得到同样的结果。 (但也许我误解了你正在寻找的东西)。 例如: >> x=[1 2 3 ; 4 5 6; 7,8,9]

x =

1 2 3

4 5 6

7 8 9

>> prod(sum(x,2))

ans =

2160

>> 1*4*7 + 1*4*8 + 1*4*9 + 1*5*7 + 1

...

对于数组值函数的integral ,您需要将'ArrayValued'属性设置为true 。 还有一些错误,你需要使用元素操作 。 请参阅下面的固定代码: q = q0 ./ (1 + x.^2);

% ↑ ↑ You need to use element-wise operations as indicated

h_func = @(t) sech(t).^2;

fun = @(t) log(1 + q*h_func(t)); %---↓------↓

...

function check_array(array $option) {

$option += array('myname' => false, 'yourname' => null);

if ($option['myname'] !== false) {

echo $option['myname'];

} else {

echo $option['yourname'];

}

}

$option = array(

'yo

...

来自: MATLAB:uint8 var = zeroes(40000000,10, 'uint8')

From: MATLAB: uint8 var = zeroes(40000000,10, 'uint8')

希望这可以帮助。 use strict;

use warnings;

my @A1 = qw'A B C D E F';

my @A2 = qw'E F G H I J';

my @A3 = qw'Q W E R T Y';

my @A4 = qw'P O L I U G';

my @matrix;

$matrix[0][4] = [ $A1[3], $A2[3] ];

# Use one of the following

for(

my $i=0;

$i

...

你创建的功能工作正常。 我必须预先分配D的大小,因为它在当前代码的每次迭代中都会有所不同。 function D = Shirinkage(a)

D = zeros(size(a));

D(1,:) = a(1,:); %this is X1

for i = 2:4

D(i,4) = 0;

for j = 1:3

D(i,j) = 0.5*(a(1,j) + a(i,j))

...

只需声明一个额外的模板参数,该参数包含有问题的类型。 template

void my_func (T (&a)[i]);

Just declare an extra template parameter, which contains the type in question. template

void my_func (T (&a)[i]);

如果要使用可变大小数组作为参数,则需要先指定大小,然后将该参数用作数组参数中的数组大小: int diagonal(int N, int A[N][N] );

int main() {

...

diagonal(N, array);

}

int diagonal(int N, int A[N][N]){

...

}

If you want to use a variable size array as an argument, you need to specify

...

你不需要this 。 param1可以定义为对象的属性,如下所示。 var app = {

param1 : null,

oneFunction: function() {

// use param1 here..

},

secondFunction: function() {

// use param1 here..

}

}

关于在函数中使用param1 ,可以将其作为this.param1访问。 this将引用ap

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值