for loop multiple variables

3 Ansichten (letzte 30 Tage)
Arashdeep Dhillon
Arashdeep Dhillon am 2 Mai 2021
Beantwortet: Jan am 2 Mai 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

Antworten (1)

Jan
Jan am 2 Mai 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')

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by