迭代函数的代码

时间:2017-07-08 05:33:02

标签: matlab loops for-loop vectorization

我想编写以下等式的Matlab代码:

z(k)=lamda*x(k)+(1-lamda)*z(k-1)

lamda可以是任何值。 x1000x22矩阵。 Z(0)=0

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

您可以使用import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Q44982690_ajax { static WebDriver driver=null; public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.cathaypacific.com/cx/en_IN.html"); WebElement leavingFrom=driver.findElement(By.xpath("//input[@id='depart-label']")); leavingFrom.click(); leavingFrom.sendKeys("Bengaluru"); // Entering the starting point WebElement cityList=driver.findElement(By.xpath("//*[contains(@id,'ui-id')]//a")); System.out.println(cityList.getText()); cityList.click(); // Selecting the required option WebElement goingTo=driver.findElement(By.xpath("//input[@id='destination-label']")); goingTo.click(); goingTo.sendKeys("Singapore"); //Entering the Destination point WebElement gCityList=driver.findElement(By.xpath("//input[@id='destination-label']//following::ul[1]/li/a")); //THIS IS WHERE XPATH IS AGAIN WORKING System.out.println(gCityList.getText()); gCityList.click(); } } 。同样的事情

iteration function

并在您的代码中调用function z = itrationFunctio(k,x,lambda) if(k == 0) z = 0; else z = lambda*x+(1-lambda)*itrationFunctio((k-1),x,lambda); end

答案 1 :(得分:0)

该矢量化解决方案是否适合您?

% parameters
x = rand(10,1);
lambda = 2;

% init z, normalize with (1-lambda)
z(2:numel(x)) = lambda/(1-lambda)*x(2:end);

% cumsum, denormalize with (1-lambda)
z = cumsum(z)*(1-lambda)

但是我不明白为什么你的x是矩阵而不是矢量。什么是z应该是什么维度k?因此,如果x表示要并行计算的几个向量,则可以起作用:

% parameters
x = rand(1000,22);
lambda = 2;

% init z, normalize with (1-lambda)
z(2:size(x,1),:) = lambda/(1-lambda)*x(2:end,:);

% cumsum, denormalize with (1-lambda)
z = cumsum(z,1)*(1-lambda)