-
Notifications
You must be signed in to change notification settings - Fork 10
/
fs_matrix_max.m
53 lines (48 loc) · 1.69 KB
/
fs_matrix_max.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%% Maximum of Matrix Columns, Sort Matrix Columns
% *back to* <https://fanwangecon.github.io *Fan*>*'s* <https://fanwangecon.github.io/Math4Econ/
% *Intro Math for Econ*>*,* <https://fanwangecon.github.io/M4Econ/ *Matlab Examples*>*,
% or* <https://fanwangecon.github.io/MEconTools/ *MEconTools*> *Repositories*
%% Max Value from a Matrix
% Given a matrix of values, what is the maximum element, what are the row and
% column indexes of this max element of the matrix.
rng(123);
N = 3;
M = 4;
mt_rand = rand(M,N);
disp(mt_rand);
[max_val, max_idx] = max(mt_rand(:));
[max_row, max_col] = ind2sub(size(mt_rand), max_idx)
%% MAX Value from Each Column
% There is a matrix with N columns, and M rows, with numerical values. Generate
% a table of sorted index, indicating in each column which row was the highest
% in value, second highest, etc. (1) sort each column. (2) show the row number
% from descending or ascending sort for each column as a matrix.
% Create a 2D Array
rng(123);
N = 2;
M = 4;
mt_rand = rand(M,N);
disp(mt_rand);
%%
% Use the maxk function to generate sorted index:
% maxk function
[val, idx] = max(mt_rand);
disp(val);
disp(idx);
%% MAXK Sorted Sorted Index for Each Column of Matrix
% There is a matrix with N columns, and M rows, with numerical values. Generate
% a table of sorted index, indicating in each column which row was the highest
% in value, second highest, etc. (1) sort each column. (2) show the row number
% from descending or ascending sort for each column as a matrix.
% Create a 2D Array
rng(123);
N = 2;
M = 4;
mt_rand = rand(M,N);
disp(mt_rand);
%%
% Use the maxk function to generate sorted index:
% maxk function
[val, idx] = maxk(mt_rand, M);
disp(val);
disp(idx);