What is the best way to define vectors based on a condition?
4 次查看(过去 30 天)
显示 更早的评论
Hello,
So I do have a function, which has the (same sized) vectors x,y,z as input parameters and a same sized k as an output parameter.
Now I want to calculate k with a simple for loop. As I come from other languages I could get it to run. However it is pretty slow and I know MATLAB has a lot of tricks for these kind of stuff.
So the minimal running example code is:
A=2.*p+1;
Bm=p+1;
Bs=p+2;
x_i=(x+y)*0.01;
x_s=x-x_i;
[r,c] = size(T);
k = zeros(r,c);
for i=1:r
if x(i)>=x_i(i)
k(i)=A(i)+Bm(i).*x_s(i);
elseif x(i)<x_i(i)
k(i) = A(i)+Bs(i).*x_s(i);
end
if k(i)==0
k(i) = 0.001;
end
end
Does MATLAB have a more efficient way to implement this?
Thanks in advance
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!