for loop multiple variables

5 views (last 30 days)
Arashdeep Dhillon
Arashdeep Dhillon on 2 May 2021
Answered: Jan on 2 May 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

Answers (1)

Jan
Jan on 2 May 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')

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!