• Forum
  • Lounge
  • How to find the largest element value fr

 
How to find the largest element value from a row of a matrix


Hi Everyone,

I need to find out the largest element of a chosen row from a matrix.

For example, qtable is a matrix.

qtable =

3 44 55
2 66 4
12 33 0

I will randomly chose a row number and then find out the maximum element corresponding to that row. For example, if randomly chosen row index is 1, the maximum element is 66


Would anyone help me with the c++ code please?

Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <valarray>
using namespace std;

using matrix = valarray< valarray<double> >;

int main()
{
   matrix qtable = { { 3, 44, 55 }, { 2, 66, 4 }, { 12, 33, 0 } };
   int row;
   cout << "Enter a row number (0-" << qtable.size() - 1 << "): ";   cin >> row;
   if ( row >= 0 && row < qtable.size() ) cout << "Maximum element is " << qtable[row].max() << '\n';
}


Enter a row number (0-2): 1
Maximum element is 66

Using a random chosen row number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <valarray>
#include <random>

using matrix = std::valarray< std::valarray<double> >;

auto Rand {std::mt19937 {std::random_device {}()}};

int main()
{
	const matrix qtable {{ 3, 44, 55 }, { 2, 66, 4 }, { 12, 33, 0 }};

	auto randNo {std::uniform_int_distribution<size_t> {0, qtable.size() - 1}};
	const auto rw {randNo(Rand)};

	std::cout << "Maximum element for row " << rw << " is " << qtable[rw].max() << '\n';
}



Maximum element for row 2 is 33

Thanks everyone.

The problem is in my compiler, this code shows errors. I'm very new in c++ and don't have enough knowledge about compiler version.

The following code for choosing random element from an array works fine in my compiler.

double state[] = {0, 1, 2, 4, 5, 10};
auto current_state_index = rand() % state_size;
auto current_state = state[current_state_index];

Now, I have to find the maximum element for row number "current_state_index" of a matrix "qtable".

Please note that the elements of 'qtable' is not fixed. In each run, the value will be updated.

Would you please suggest me a code that will work in my compiler?
Last edited on
What compiler are you using on which OS? The current standard is C++17 with C++20 imminent. The compiler used should have some method of setting the C++ standard to use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <valarray>
#include <ctime>
#include <cstdlib>
using namespace std;

using matrix = valarray< valarray<double> >;

int main()
{
   srand( time( 0 ) );
   matrix qtable = { { 3, 44, 55 }, { 2, 66, 4 }, { 12, 33, 0 } };
   int row = rand() % qtable.size();
   cout << "Row: " << row << "    Maximum: " << qtable[row].max() << '\n';
}

It's ubuntu 19.04
g++ (Ubuntu 8.3.0-6ubuntu1) 8.3.0
Thank you @lastchance. I will try.
@seeplus and @lastchance, can anyone advice me after finding the maximum element for a row, how to find the corresponding column index?

I meant, for example, if we get maximum element for row 2 is 33, how to code to find the column no. corresponding to 33?

Thanks for your help
Last edited on
You could either write a for-loop to do it (in which case you might as well get the maximum at the same time) or you could use std::max_element.

Either way, I think you should state ALL your requirements up front, and indicate what you are ultimately trying to do, rather than asking new things in dribs and drabs. It might influence what containers one would use to store the matrix.
@lastchance, Yes, I got your point. Many thanks for answering my questions.
Actually, I wrote my code in matlab and trying to write the same code in c++. As i do not have any experience in c++, i'm trying to code it part by part and checking if each small part works. I have to code a proposed algorithm where i will need these things.

Would you please help me with one more question?

With your above suggested code,

matrix qtable = { { 3, 44, 55 }, { 2, 66, 4 }, { 12, 33, 0 } }; //initial value

I am trying to update one particular element value of qtable matrix .
For example the element value in position qtable(1,2). This position is not fixed to (1,2) all the time. I will input row and col index, and update the corresponding element value.

I tried in following way but got error

qtable[row_index][col_index] = new_value

Any suggestion please?
You would need to show your actual code, tell us what "error" is, and state what values of row_index and col_index you are using.

"a proposed algorithm" is also not telling us what you are trying to do.
Last edited on
I tried in following way but got error

qtable[row_index][col_index] = new_value


Has qtable been defined as const? const means that the variable value(s) cannot be changed after initialisation.

To obtain the max element number and element and to update the data, consider:

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
#include <iostream>
#include <valarray>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

using matrix = valarray< valarray<double> >;

int main()
{
	srand(time(0));

	matrix qtable = {{ 3, 44, 55 }, { 2, 66, 4 }, { 12, 33, 0 }};
	const int row = rand() % qtable.size();
	auto mx = std::max_element(std::begin(qtable[row]), std::end(qtable[row]));

	cout << "Random row " << row << '\n';
	cout << "Row: " << row << "    Maximum: " << *mx << " at " << mx - std::begin(qtable[row]) << '\n';

	mx = std::max_element(std::begin(qtable[1]), std::end(qtable[1]));
	cout << "\nRow: " << 1 << "    Maximum: " << *mx << " at " << mx - std::begin(qtable[1]) << '\n';

	cout << "Setting max in row 1 to 99\n";

	qtable[1][2] = 99;

	mx = std::max_element(std::begin(qtable[1]), std::end(qtable[1]));
	cout << "Row: " << 1 << "    Maximum: " << *mx << " at " << mx - std::begin(qtable[1]) << '\n';
}



Random row 2
Row: 2    Maximum: 33 at 1

Row: 1    Maximum: 66 at 1
Setting max in row 1 to 99
Row: 1    Maximum: 99 at 2

Last edited on
> using matrix = valarray< valarray<double> >;

Avoid this: using a matrix of this kind would engender undefined behaviour.

std::valarray<double> does not meet the requirements of NumericType,
(operations on it may throw exceptions.)
see: https://en.cppreference.com/w/cpp/named_req/NumericType

If high performance numeric matrix operations are required, use a specialised library like Eigen or boost uBlas.
@seeplus, qtable is not defined as const.

For updating matrix element, the row index and column index are actually variable.

Here is my code. I'm getting error for the bold commands.


#include <iostream>
#include <string>
#include <string.h>
#include <valarray>
#include <ctime>
#include <cstdlib>


using namespace std;

using matrix = valarray< valarray<double> >;


double state[] = {0, 1, 2};
double action_new[] = {44, 20.1036, 102.2713, 100.4267};

int state_size = sizeof(state)/sizeof(state[0]);
int action_size = sizeof(action_new)/sizeof(action_new[0]);

matrix qtable={ { 3, 44, 55, 6 }, { 2, 66, 4, 3 }, { 12, 33, 0, 1 } };


int main() {


cout<<"QTable = "<<endl;
for (int i = 0; i < state_size; ++i) {
for (int j = 0; j < action_size; ++j) {
cout << qtable[i][j] << "\t";
}
cout << endl;
}

for(int episode =1; episode<=5; ++episode)
{
//srand( time( 0 ) );
auto current_state_index = rand() % state_size;
auto current_state = state[current_state_index];

cout <<" current state index = " << current_state_index <<endl;
cout <<" current state = " <<current_state << endl;


auto chosen_action_value = qtable[current_state_index].max();

//finding index of the chosen action value

for (int m=0; m<action_size; ++m)
{
if(action_new[m]==chosen_action_value) {
auto chosen_action_index = m;
break;
}
}

//update Qtable element with row = current_state_index and col = chosen_action_index

qtable[current_state_index][chosen_action_index] = qtable[current_state_index][chosen_action_index] + 2;



}
srand( time( 0 ) );

cout<<"Updated QTable = "<<endl;
for (int i = 0; i < state_size; ++i) {
for (int j = 0; j < action_size; ++j) {
cout << qtable[i][j] << "\t";
}
cout << endl;
}


}





error: ‘chosen_action_index’ was not declared in this scope
qtable[current_state_index][chosen_action_index] = qtable[current_state_index][chosen_action_index] + 2;


Would you suggest me the correct command?

Thank you
Last edited on
@Feit,
Your error message tells you immediately what is wrong - you haven't declared a variable called chosen_action_index (within the current scope).... so you can't expect to use it. You have declared it within a small if block, to which its scope is confined.

c++ doesn't work like matlab.

We keep asking: what is your code intended to do?
Last edited on
@Feit,
Are you trying to code up Q-learning/reinforcement learning?
https://towardsdatascience.com/simple-reinforcement-learning-q-learning-fcddc4b6fe56
https://en.wikipedia.org/wiki/Q-learning

If so, why don't you just say so?

Seems like machine learning is coming up quite a lot in the forum at the moment.
Last edited on
@lastchance, you are right. My ultimate goal is to implement reinforcement learning. But at this stage, I am just trying to learn some basic codes in c++ that will be helpful to code RL.

Thank you!
Topic archived. No new replies allowed.