Expression representation in an Iteration

1 次查看(过去 30 天)
I'd love to know how i can express the expression in a loop. n is the iteration number. I know it may seem trivial but i've been cracking my skull over it. My approach, which I figure is wrong anyways, was to index the second term with n. But the iteration number is greater than the the matrix elements and i get an error. Here is my attempt at the issue.
w=[randn(2,1)+ij*randn(2,1)];
W_k=w*ctranspose(w);
for n=1:N;
w=[randn(2,1)+ij*randn(2,1)];
W_k1=w*ctranspose(w);
t=W_k - W_k1;
end
Is this doing what the expression requires?

采纳的回答

Walter Roberson
Walter Roberson 2020-8-6
W_k = cell(N,1);
w=[randn(2,1)+ij*randn(2,1)];
W_k{1} = w*ctranspose(w);
for n=1:N;
w = randn(2,1)+ij*randn(2,1);
W_k{n+1} = w*ctranspose(w);
t = W_k{n} - W_k1{n+1};
%do something with t
end
  1 个评论
Vezamafa Nzima
Vezamafa Nzima 2020-8-6
Thank you very much. This exactly what i was looking for. Makes sense even just going through the code.

请先登录,再进行评论。

更多回答(1 个)

Sudheer Bhimireddy
Something like this ?
w_k(nx) = 0;
w_k_n(n_iter) = 0;
for i=1:nx
w_k_n(1) = w_k(i);
for j=1:n_iter
your_ans(i) = w_k(i) - w_k_n(j);
end
end
  2 个评论
Vezamafa Nzima
Vezamafa Nzima 2020-8-6
Unfortunately it's not working. W_k and W_k_n should be matrices of the same size. The way i figure it also is that the iteration should not be limited by the size of the matrix. That is, i'd love the iteration to go on as many times as i choose.
Sudheer Bhimireddy
If both matrices are of same size and you want to iterate as many times as you want, then you can do one of the below two things:
1) Dynamically increase the size of both matrices as you iterate and fill the w_k with repetetive values. This way you will end up with redundant data.
OR
2) Over-write the w_k_n matrix once the iteration limit has crossed the size of the matrix. This way you might loose some data once you start overwriting.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by