使用矩阵输入Matlab的Linspace [英] Linspace using matrix input matlab

查看:80
本文介绍了使用矩阵输入Matlab的Linspace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题似乎很奇怪,因为我知道我们不能在linspace(x1,x2,n)函数中使用矩阵作为输入.但是我的问题更多是这样的:我有一个向量A=linspace(0,Amax,N),我想建立一个向量B_k或大矩阵B_k=linspace(0,A(k),N)的序列,但又不作任何for循环来减慢整个计算的速度.

My question seems wierd, because I know that we can't use a matrix as input in linspace(x1,x2,n) function. But my question is more something like : I have a vector A=linspace(0,Amax,N), and I want to build a serie of vector B_k or big matrix B_k=linspace(0,A(k),N) but without making a for loop that slows down my whole calculation.

% already defined A
rGC=linspace(0,75e-7,N);
for k=1:N
    r=rGC(k);
    v=linspace(0,A*r,N);
    y=f(r,v);
    INT=trapz(v,y);
    %  The same for 8 more integrals
end

推荐答案

也许使用interp1进行插值,例如:

Maybe something using interp1, to interpolate something like:

[0    0    0    ... 0 ]
[A(1) A(2) A(3) ... A(N)]

具有N行....例如:

with N rows .... For instance:

N = 5;
Amax = 15;
A = linspace(0, Amax, N);

x = [0 1];
y = zeros(2, N);
y(2, :) = A;

B = interp1(x, y, linspace(0, 1, N))

哪个会给:

B =

     0         0         0         0         0
     0    0.9375    1.8750    2.8125    3.7500
     0    1.8750    3.7500    5.6250    7.5000
     0    2.8125    5.6250    8.4375   11.2500
     0    3.7500    7.5000   11.2500   15.0000

不确定它会比for循环快多少,或者甚至可以明白这里的意思:)

Not sure it will be any faster than a for loop or that even get the point here :)

这篇关于使用矩阵输入Matlab的Linspace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆