for loop multiple variables

1 次查看(过去 30 天)
Arashdeep Dhillon
回答: Jan 2021-5-2
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

回答(1 个)

Jan
Jan 2021-5-2
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')

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by