Change or update an existing variable at iteration intervals in an iteration loop

2 vues (au cours des 30 derniers jours)
Ayobami Meadows le 18 Déc 2021
I need to update the variable t with this same initilization formula when the iteration gets to iteration 10, iteration 15, iteration 25 and iteration 40. I tried this way but no changes.
clc;clear;close all;
MaxIt=50;
P=3;
xmin = randi(10);
ymin = 100;
x = [150 300 350];
y = [500 700 850];
%% Initialize variable t
t=zeros(1,P);
for i=1:P
t(i)=sqrt((y(i)-ymin)+(x(i)-xmin));
end
%% Main loop
for it=1:MaxIt
F = t * pi * randi(10);
GetCost = F(1);
disp(['MaxIt' num2str(it) ': Get Cost =' num2str(Fanswer(it))]);
%<------ This is where my problem is--------->
% I need to calculate and update variable t again here after 10th,
% 15th, 25th and 40th iterations. I did it this way but didn't work.
if (it)==[10 15 25 40]
for i=1:P
t(i)=sqrt((y(i)-ymin)+(x(i)-xmin));
end
end
end
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponse acceptée

Sargondjani le 18 Déc 2021
it==[10 15 25 40]
gives a vector as output. You want a scalar either 0 or 1:
Use:
ismember(it,[10 15 25 40])
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by