How can I solve this matrix question using for loop?

3 ビュー (過去 30 日間)
Sena
Sena 2022 年 11 月 27 日
編集済み: Torsten 2022 年 11 月 27 日
Dear all,
There is a problem that I need to solve in the attached picture. First I defined the benefit matrix as A, and then I set up a for loop and tried to find the newly formed benefit matrices in the tables for each user from 1 unit of water to 8 units of water with this code. However, I think I'm making a math error because it's not giving the correct result. I need to calculate the benefits for each user and show the maximum and how many units of water should go to each user as a result. How can I do it?
clc;
clear;
close all;
A=[6 5 7;12 14 30;35 40 42;75 55 50;85 65 60;91 70 70;96 75 72;100 80 75]; %the matrix of benefits for each user
%for 3 users
for i=1:8
v(i)=A(i,3)
%for 2 users
for j=1:8
v(j)= A(j,2);
%for 1 user
for k=1:8
v(k)=A(k,1);
end
end
end

採用された回答

Torsten
Torsten 2022 年 11 月 27 日
編集済み: Torsten 2022 年 11 月 27 日
Not the most efficient solution, but maybe best to understand what's happening.
A=[0 0 0 ;6 5 7;12 14 30;35 40 42;75 55 50;85 65 60;91 70 70;96 75 72;100 80 75];
max_benefit = -Inf;
index_user = zeros(1,3);
units_of_water = 8;
for i = 0:units_of_water
for j = 0:units_of_water
for k = 0:units_of_water
if i+j+k <= units_of_water
benefit = A(i+1,1) + A(j+1,2) + A(k+1,3);
if benefit > max_benefit
units_of_water_per_user = [i, j, k];
max_benefit = benefit;
end
end
end
end
end
max_benefit
max_benefit = 130
units_of_water_per_user
units_of_water_per_user = 1×3
4 4 0
  1 件のコメント
Sena
Sena 2022 年 11 月 27 日
thank you for reply. I'll try again with your answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by