フィルターのクリア

for loop multiple variables

2 ビュー (過去 30 日間)
Arashdeep Dhillon
Arashdeep Dhillon 2021 年 5 月 2 日
回答済み: 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by