Repeat element in matrix 2D

I failed to print out matrix MX,what wrong with my code?

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 #include <stdio.h>
#include <iostream>
#include <cmath>
#include <conio.h>//define getch
#include <iomanip>
#include <fstream>
#include <string>
#include <map>
#include <random>
#include <chrono>
#include <cstdlib>
#include<algorithm>

#define m 10

using namespace std;
void main()
{
	int i, j,max,M,KMAX,L=0,A=0,k=1;
	int a[m];
	//double MX;
	double p = 0.95;
	double a1 = 20.79, a2 = 0.005;
	int Vmax = 3, KM = 5;
	cout.setf(ios::fixed);
	cout.precision(3);

	//////////////////////////////////////////////////////////////////////////////////////////
	//value of X-axis
	/////////////////////////////////////////////////////////////////////////////////////////

	double X_axis[m];
	for (i = 1; i <= m; i++)
	{
		X_axis[i] = ((i - 1) / 4.0);
		cout << "   " << X_axis[i] << endl;
	}

	///////////////////////////////////////////////////////////////////////////////////////
	//random number follow poisson distribution
	//////////////////////////////////////////////////////////////////////////////////////
	cout << "some Poisson-distributed results:" << endl;

	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); 
	std::default_random_engine generator(seed);
	std::poisson_distribution<int> distribution(2 * X_axis[m]);

	for (int i = 0; i < m; ++i)
	{
		a[i] = distribution(generator);
		std::cout << i << "   " << a[i] << "  " << endl;
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	//maximum number of DSB
	///////////////////////////////////////////////////////////////////////////////////
	max = a[1];
	for (int i = 0; i < m; ++i)
	{
		if (max <= a[i])
			max = a[i];
		KMAX = max;
	}
	cout << "maximum number is " << KMAX << endl;
	
    /////////////////////////////////////////////////////////////////////////////////////////
	//matrix
	////////////////////////////////////////////////////////////////////////////////////////
    
	unsigned short int size = KMAX;
	std::vector<int> vec(size);

	std::cout << "Size of vector is: " << vec.size() << '\n';
	vector<int> P;
	for (int i = 0; i < vec.size(); i++)
	{
	    double	P = (p*Vmax*i) / (KM + i);
		double  Q = (1 - p)*Vmax*i / (KM + i);
		cout << "P[" << i << "] = " << P << "\t" << "Q[" << i << "] = " << Q << endl;
	}
	
	int M = ((vec.size() + 1)*(vec.size() + 2)) / 2;

	vector<vector<int>> MX;
	

	for (i = vec.size(); i >= 1; i--) ;// Position P in MX matrix
	{
		for (j = 1; j <= i; j++)
		{
			
			MX[j - 1 + L][vec.size() + k] = P[A + 1];
			cout << "MX[" << j - 1 + L << "][" << vec.size() + k << "] = " << MX[i][j] << endl;
			k++;
		}
		cout<<endl;
		L = k + A;
		A++;

	}



	_getch();
}
I failed to print out matrix MX,what wrong with my code?

Can you tell us what is a matrix MX?
Last edited on
I wonder if you could fix a few things first and then re-post (as a new post) - this code is very difficult to get to compile.

- please use int main(), not void main();

- variable M is declared twice:
int i, j,max,M,KMAX,L=0,A=0,k=1;

int M = ((vec.size() + 1)*(vec.size() + 2)) / 2;

- ARRAYS COUNT FROM 0, NOT 1 - there are huge inconsistencies in your code here.
1
2
3
4
5
6
	double X_axis[m];
	for (i = 1; i <= m; i++)    // <=== bound to go beyond the end of the array
	{
		X_axis[i] = ((i - 1) / 4.0);
		cout << "   " << X_axis[i] << endl;
	}


std::poisson_distribution<int> distribution(2 * X_axis[m]); // <=== X_axis[m] doesn't exist

max = a[1]; // <==== probably meant a[0]

1
2
3
for (i = vec.size(); i >= 1; i--) ;//  <==== this is going to exceed array bounds
	{
		for (j = 1; j <= i; j++)



- what exactly are you declaring P as? A vector of ints or a double?
1
2
3
4
5
6
7
vector<int> P;
	for (int i = 0; i < vec.size(); i++)
	{
	    double	P = (p*Vmax*i) / (KM + i);
		double  Q = (1 - p)*Vmax*i / (KM + i);
		cout << "P[" << i << "] = " << P << "\t" << "Q[" << i << "] = " << Q << endl;
	}



- your for loop ends ... at the semicolon at the end of the line
for (i = vec.size(); i >= 1; i--) ;


- when you first declare vectors they don't have any content (unless you specified it in their constructor) - yet you are using them as if they are fully built. Applies to P and MX.


Please fix all these problems first.
Last edited on
Matrix MX is 2D matrix where the size depend on M, and M depend on vec.size() which is KMAX .Element P and Q will be call into matrix MX but in certain position,let say vec.size() is 2, so the matrix MX[6][6],then my final output would be like this where the rest element is 0,


1
2
3
4
5
6
7
 MX[0][0]=0   MX[0][1]=0  MX[0][2]=0   MX[0][3]=P[1]   MX[0][4]=0           MX[0][5]=0
 MX[1][0]=0   MX[1][1]=0  MX[1][2]=0   MX[1][3]=Q[1]   MX[1][4]=P[1]      MX[1][5]=0                                
 MX[2][0]=0   MX[2][1]=0  MX[2][2]=0   MX[2][3]=0        MX[2][4]=Q[1]     MX[2][5]=0
 MX[3][0]=0   MX[3][1]=0  MX[3][2]=0   MX[3][3]=0        MX[3][4]=0          MX[3][5]=P[2]
 MX[4][0]=0   MX[4][1]=0  MX[4][2]=0   MX[4][3]=0        MX[4][4]=0          MX[4][5]=Q[2]
 MX[5][0]=0   MX[5][1]=0  MX[5][2]=0   MX[5][3]=0        MX[5][4]=0          MX[5][5]=0
 


previously my KMAX is int constant, then i changed the programme where KMAX is maximum number from random number, so I can't create arrays from random numbers generated at run time, because my KMAX depend on random number, then i did this

1
2
unsigned short int size = KMAX;
std::vector<int> vec(size);


only then i can print out P and Q,and this is the first time i'm using std::vector.Below is the code i put P and Q in matrix MX, for KMAX int contant.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (i = vec.size(); i >= 1; i--) ;// Position P in MX matrix
	{
		for (j = 1; j <= i; j++)
		{
			
			MX[j - 1 + L][vec.size() + k] = P[A + 1];
			cout << "MX[" << j - 1 + L << "][" << vec.size() + k << "] = " << MX[i][j] << endl;
			k++;
		}
		cout<<endl;
		L = k + A;
		A++;

	}

the output should be like this

1
2
3
4
5
6
MX[0][3]=P[1]   
MX[1][4]=P[1]
MX[1][3]=Q[1]
        
MX[3][5]=P[2]
MX[4][5]=Q[2]


I changed KMAX to vec.size() in the for loop since it works for P and Q.what should i do for matrix MX?
Can you post your WHOLE code, please.

You still haven't removed the stray semicolon below (just before //)
for (i = vec.size(); i >= 1; i--) ;// Position P in MX matrix
Sorry for pending, this is my whole matrix

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <unsupported/Eigen/MatrixFunctions>
#include <iostream>
#include <math.h>
#include <conio.h>//define getch
#include <iomanip>
#include <fstreaM>
#include <string>
#include <map>
#include <random>
#include <chrono>
#include <vector>

//#define KMAX 2
//#define M ((KMAX+1)*(KMAX+2))/2
#define m 10



using namespace std;
using namespace Eigen;

void main()

{
	int k = 1, L = 0, A = 0, i, j, e,max,M;
	int z = 1, R = 0, C = 0; //variable for Q
	int B = 0, T = 0, c = 1, q = 1, n = 0, H = 0,o = 0,u = 0,t=24,F; // variable for diagonal
	
	//double P[KMAX];
	//double Q[KMAX];
	//double S[KMAX];
	
	//double E[M][M];
 	
	//double Matrix[M + 1][M + 1];
	//double I[M + 1][M + 1];
	int a[m];
	//double MX[M][M];
	double p = 0.95;
	double a1 = 20.79, a2 = 0.005;
	int Vmax = 3, KM = 5;
	cout.setf(ios::fixed);
	cout.precision(3);

	//////////////////////////////////////////////////////////////////////////////////////////
	//value of X-axis
	/////////////////////////////////////////////////////////////////////////////////////////

	double X_axis[m];
	for (i = 1; i <= m; i++)
	{
		X_axis[i] = ((i - 1) / 4.0);
		cout << "   " << X_axis[i] << endl;
	}

	///////////////////////////////////////////////////////////////////////////////////////
	//random number follow poisson distribution
	//////////////////////////////////////////////////////////////////////////////////////
	cout << "some Poisson-distributed results:" << endl;

	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine generator(seed);
    std::poisson_distribution<int> distribution(2*X_axis[m]);

	int KMAX;

	for (int i = 0; i < m; ++i)
	{
		a[i] = distribution(generator);
		std::cout << a[i] << "  " << endl;
	}
	
    ////////////////////////////////////////////////////////////////////////////////////
    //maximum number of DSB
    ///////////////////////////////////////////////////////////////////////////////////
	
	max=a[1];
	for (int i = 0; i < m; ++i)
	{
		if (max <= a[i])
			max = a[i];
		KMAX = max;
		
	}
	cout << "maximum number is " << KMAX << endl;

	
	unsigned short int size = KMAX;
	std::vector<int> vec(size);

	std::cout << "Size of vector is: " << vec.size() << '\n';

	for (int i = 0; i < vec.size(); i++)
	{
		double	P = (p*Vmax*i) / (KM + i);
		double  Q = (1 - p)*Vmax*i / (KM + i);
		cout << "P[" << i << "] = " << P << "\t" << "Q[" << i << "] = " << Q << endl;
	}

/////////////////////////////////////////////////////////////////////////////////////////
//matrix
////////////////////////////////////////////////////////////////////////////////////////
	
	int M=((KMAX + 1)*(KMAX + 2)) / 2;

	for (i = 0; i <= M; i++)//matrix 0
		for (j = 0; j <= M; j++);
	 MX[M][M]=0;
	
	
	
	

	for (i = KMAX; i >= 1; i--)// position P in matrix
	{
		for (j = 1; j <= i; j++)
		{
			MX[j - 1 + L][KMAX + k] = P[A + 1];
			//cout <<"MX["<<j-1+L<<"]["<<KMAX+k<<"] = "  <<MX[j-1+L][KMAX+k]<<endl;
			k++;
		}
		//cout<<endl;
		L = k + A;
		A++;

	}


	for (i = KMAX; i >= 1; i--)// position Q in matrix
	{
		for (j = 1; j <= i; j++)
		{
			MX[j + R][KMAX + z] = Q[C + 1];
			//cout <<"MX["<<j+R<<"]["<<KMAX+z<<"] = "  <<MX[j+R][KMAX+z]<<endl;
			z++;
		}
		//cout<<endl;
		R = z + C;
		C++;

	}

   for (i = KMAX + 1; i >= 1; i--)// calculation for diagonal

	{
		for (j = 1; j <= i; j++)
		{
			MX[B + c - q][B + c - q] = -(a1*double(j - 1) + a2*double(B)*double(B)) - ((Vmax*double(B)) / (KM + double(B)));
			//cout <<"MX["<<B+c-q<<"]["<<B+c-q<<"] = "<<  MX[B+c-q][B+c-q] << endl;
			c++;

		}
		q++;
		B++;
	}


	cout << "Here is the matrix:" << endl; //double array matrix
	for (i = 0; i < M; i++)
	{
		for (j = 0; j < M; j++)
		{
			//	cout << setw(7) << MX[i][j] << "  ";
		}
		//cout << endl;
	}

////////////////////////////////////////////////////////////////////////////////
	//solve matrix exponential
///////////////////////////////////////////////////////////////////////////////

	MatrixXd result(M, M);  //apply eigen matrix
	for (int i = 0; i < M; i++)
		for (int j = 0; j < M; j++)
			result(i,j)=MX[i][j]*t;
	cout << "     " << result << endl;

	EigenSolver<MatrixXd> es(result);
	MatrixXd D = es.pseudoEigenvalueMatrix();
	MatrixXd V = es.pseudoEigenvectors();
	cout << "The pseudo-eigenvalue matrix D is:" << endl << D << endl;
	cout << "The pseudo-eigenvector matrix V is:" << endl << V << endl;
	cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;*/
	
		_getch();
}
There isn't much chance of me compiling this, because I haven't got access to most of your called eigenfunction routines. However, I could take that part out and look at the rest.

Your matrix initialisation is so obscure that I can't work out what you are intending. However, I can see lots of potential issues. In lines 119 and 134 you are trying to use arrays P[] and Q[], but no such arrays exist. Sure, you create some SCALARS P and Q in lines 96-97, but these are not arrays of numbers.

I mentioned far back that lines 51 to 55 are going to write beyond the end of an array.

I'm pretty sure that lines 107-109 aren't going to initialise MX as you presumably intended.

Topic archived. No new replies allowed.