Double indexing in FOR Loop
Show older comments
Hello, friends
There is a problem that I cannot solve. I know that the code below is not right. I wrote it like that to be more understandable what I need to do. I need to create a cycle that will make new polynomial functions with indexing (p1,p2,p3..and so on). Each new polynom is using previous polynom for calculating the new members, which already have their own indexing (p(1),p(2)..), so MatLab doesnt let me to provide 2 indexing at the same time. Could you please help me somehow? Thank you very much and have a nice day!
p0 = app.PolynomEditField.Value;
p0 = str2num(p0);
for i = 1:(length(p0)-1)
p{i} = p0;
p{i}(1) = (p{i-1}(1))^2;
p{i}(2) = (-1)*((p{i-1}(2))^2-2*(p{i-1}(1))*(p{i-1}(3)));
p{i}(3) = (p{i-1}(3))^2 - 2*(p{i-1}(2))*(p{i-1}(4)) + 2*(p{i-1}(1))*(p{i-1}(5));
p{i}(end-1) = ((p{i-1}(end-1))^2-2*(p{i-1}(end-2))*(p{i-1}(end)))*((-1)^(length(p{i-1})-2));
p{i}(end) = ((p{i-1}(end))^2)*((-1)^(length(p{i-1})-1));
res = char(sprintf(['Posloupnost polynomů: %s'],p{i}));
app.VsledkyTextArea.Value = res;
end
2 Comments
Rishik Ramena
on 16 Mar 2021
Do you need values from the previous iteration, or just another loop variable that increments with the loop?
Macarena Santillan
on 7 Jul 2021
I have a similar issue. I need values from previus iteration and another loop variable that increments with the loop but they depend on previous values. How would you do it for each/both cases? Thank you!
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!