本文介绍了如何在Matlab中编写指标函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是matlab的新用户,我想解决以下问题:

I am a new user of matlab and I want to tackle the following problem:

我想构造一个分段常量函数f. f应该是像f=@(t)1[0,0.25)(t)这样的匿名函数.但是,分段常数函数的间隔数通常不是固定的.相反,分段间隔取决于用户输入.

I want to construct a piecewise constant function f. f should be an anonymous function like f=@(t)1[0,0.25)(t). However, the number of intervals for the piecewise constant function is not fixed in general. Instead, the piecewise interval depends on users input.

例如,如果输入4,则分段间隔变为

For example, if one enters 4, the piecewise interval becomes

然后

如果输入5,则分段间隔变为

While if one enters 5, the piecewise interval becomes

有什么好的方法可以解决这个问题?

Are there any good ways to tackle the problem?

推荐答案

假定已经定义了权重a1,...,ak,则可以使用以下方法:

Assuming that the weights a1,...,ak are already defined, you can use the following approach:

%defines weight vector. for example: a1=1, a2=2, a3=3, a4=4,a5=5
A = 1:5;
%defines a range vector
ranges = [0:(1/length(A)):1,inf];
%The padding is for handling cases where t<0 or t>=1
APadded = [0,A,0];
%define f
f=@(t)APadded(find(t<ranges,1,'first'))

结果:

f(0.1) = 1
f(0.3) = 2
f(0.5) = 3
f(0.7) = 4
f(0.9) = 5
f(-0.1) = f(1.1) = 0;

这篇关于如何在Matlab中编写指标函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:49
查看更多