Azzera filtri
Azzera filtri

for loop multiple variables

2 visualizzazioni (ultimi 30 giorni)
Arashdeep Dhillon
Arashdeep Dhillon il 2 Mag 2021
Risposto: Jan il 2 Mag 2021
Hi, I'm trying to calculate the sum of f for each set of x and y (they are specifically in a pair so for example x(1) = 32 and y(1) = 28 and they can't be mixed up like x(1) and y(2)). I want the for loop to find the sum of f for each u and v. And then i want to see which u and v has the minimum f, u and v are a pair of coordinates. I want to see which coordinates has the minimum f.
T = readtable('Task1.xlsx','Range','A7:F106');
q = table2array(T(1:99,4));
x = table2array(T(1:99,2));
y = table2array(T(1:99,3));
cost = T(1:99,6);
u = [2:100];
v = [2:100];
a = length(u);
for i = 1:length(u)
f(i) = sum(q.*((u-x).^2) + ((v-y).^2))
end

Risposte (1)

Jan
Jan il 2 Mag 2021
Maybe:
f = zeros(numel(u), numel(v));
for i = 1:length(u)
for j = 1:length(v)
f(i, j) = sum(q.*((u(i) - x).^2) + ((v(j) - y).^2));
end
end
[minValue, minIndex] = min(f, [], 'all')

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by