Solving Matrix Index Problem

5 ビュー (過去 30 日間)
Zaki
Zaki 2013 年 9 月 30 日
編集済み: Stephen23 2023 年 7 月 19 日
is anyone here know how to solve matrix index problem in matlab?? For example the following problem :
You have a matrix for which each row is a person and the column represent the number of quarters, nickels, dimes, and pennies. What is the raw index of the person with the most money?
Note for those unfamiliar with American coins ; quarter = $ 0.25, dime = $ 0.10, nickel = $ 0.05, penny = $0.01
Example :
Input a = [1 0 0 0 ; 0 1 0 0] output b = 1
since the first person will have $ 0.25 and the second person will have only $0.05
Thanks you, i need the answer..

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 30 日
c=[0.25 0.1 0.05 0.01]
a = [1 0 0 0 ; 0 1 0 0]
[max_money,b]=max(sum(bsxfun(@times,c,a),2))
  1 件のコメント
Zaki
Zaki 2013 年 9 月 30 日
移動済み: Stephen23 2023 年 7 月 19 日
Thanks for your help

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

その他の回答 (4 件)

Rahul Sharma
Rahul Sharma 2020 年 2 月 8 日
clear all
clc
a=randi(4,3,4)
k=[0.25 0.10 0.05 0.01];
for j=1:4
for i=1:3
a(i,j)=k(j)*a(i,j)
end
end
b=max(sum(a'))
disp(b)

Rahul Sharma
Rahul Sharma 2020 年 2 月 8 日
a=randi(4,3,4)
a(:,1)=0.25*a(:,1);
a(:,2)=0.10*a(:,2);
a(:,3)=0.05*a(:,3);
a(:,4)=0.01*a(:,4);
disp(a)
b = max(sum(a'));
disp(b)

Mohana Segaran
Mohana Segaran 2023 年 7 月 19 日
a = [1 0 0 0; 0 1 0 0; 1 1 1 0];
b = [0.25 0.1 0.05 0.01; 0.25 0.1 0.05 0.01; 0.25 0.1 0.05 0.01];
x = dot(a,b,2);
[y z]= max(x);
z

Stephen23
Stephen23 2023 年 7 月 19 日
編集済み: Stephen23 2023 年 7 月 19 日
The simple and efficient approach is to use MTIMES:
M = [1,0,0,0;0,1,0,0;0,2,1,1] % any number of rows
M = 3×4
1 0 0 0 0 1 0 0 0 2 1 1
V = [0.25;0.1;0.05;0.01]; % one column
[~,X] = max(M*V)
X = 3

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by