Expression representation in an Iteration
조회 수: 2 (최근 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/342145/image.png)
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?
댓글 수: 0
채택된 답변
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개)
Sudheer Bhimireddy
2020년 8월 6일
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
Sudheer Bhimireddy
2020년 8월 6일
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 Center 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!