Store sctrucs in a matrix

Hi,

how can I store a struct in a matrix?

Supose you have

1
2
3
4
5
    struct vectors {

        double module,phase;

    };


I want vector[i][j].module and vector[i][j].phase...
Last edited on
The same way you make a matrix with ints and other basic types:

1
2
3
4
5
6
7
8
9
10
11
12
// to make with 'int'
int intmatrix[4][4];
intmatrix[0][0] = 1;

// to make with a struct
struct mystruct
{
  double module, phase;
};

mystruct structmatrix[4][4];
structmatrix[0][0].phase = 1.0;
Hi Disch,

I understand your reply and it solves the question, but...

Imagine that I have done such "matrix of structs" inside a method, how can I do to pass structmatrix from a method back to main()?

Here is the program I am working:

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* 
 * File:   main.cpp
 * Author: Cristiano Strieder // cstrieder@gmail.com
 *
 * Created on April 22, 2009, 11:32 AM
 */

#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>

using namespace std;

int lines, cols; // Store the size(lines and cols) of the matrix

/*
 * 
 */

int matrix_lines(istream &iFile) {
// Método para obter o número linhas da matriz.
    iFile.clear();
    iFile.seekg(0);
    int lines = 0;
    string line;
    while(!iFile.eof()) {
        getline (iFile, line);
        lines++;
    }
//    cout << "Número de linhas: " << lines-1 << endl;
    return lines-1;
}

int matrix_cols(istream &iFile) {
// Método para obter o número de colunas da matriz.
    iFile.clear();
    iFile.seekg(0);

    string line;
    double n;
    int cols = 0;

    getline (iFile, line);
//    cout << line << endl;

    stringstream is(line);
    n = 0;
    while (is >> n) {
//        cout << n << endl;
        cols++;
    }


//
//    cout << "Número de colunas: " << cols << endl;
    return cols;
}

vector<vector<double> > matrixRead(istream &iFile) {
// Método para ler a matriz.

    vector<vector<double> > matrix( cols , vector<double>( lines ) );

    double value;
    iFile.clear();
    iFile.seekg(0);
    
    for(int i = 0; i < lines; i++){
        for(int j = 0; j < cols; j++){
           matrix[i][j]  = (iFile >> value,value);
           cout << matrix[i][j] << " ";
        }
        cout << "\n";
    }

    return matrix;
}

vector<vector<double> > gradX(vector<vector<double> > matrix) {

    vector<vector<double> > grad_x( cols , vector<double>( lines ) );

    for(int i = 0; i < lines; i++) {
        for(int j = 0; j < cols; j++) {

            if(j==0) { // primeira coluna
                grad_x[i][j] = matrix[i][1] - matrix[i][0];
            }else if(j==cols-1) { // ultima coluna
                grad_x[i][j] = matrix[i][j] - matrix[i][j-1];
//                cout << "matrix[i][j-1]= " << matrix[i][j-1] << " matrix[i][j]= " << matrix[i][j] << endl;
            }else // restante da matriz
                grad_x[i][j] = (matrix[i][j+1] - matrix[i][j-1])/2;
        }
    }
    cout << "gradiente X" << endl;

    for(int i = 0; i < lines; i++){  // Output gradient X
        for(int j = 0; j < cols; j++){
           cout << grad_x[i][j] << " ";
        }
        cout << "\n";
    }

    return grad_x;
}

vector<vector<double> > gradY(vector<vector<double> > matrix) {

    vector<vector<double> > grad_y( cols , vector<double>( lines ) );

    for(int i = 0; i < lines; i++) {
        for(int j = 0; j < cols; j++) {

            if(i==0) { // primeira linha
                grad_y[i][j] = matrix[1][j] - matrix[0][j];
            }else if(i==lines-1) { // ultima linha
                grad_y[i][j] = matrix[i][j] - matrix[i-1][j];
            }else // restante da matriz
                grad_y[i][j] = (matrix[i+1][j] - matrix[i-1][j])/2;
        }
    }
    cout << "gradiente Y" << endl;

    for(int i = 0; i < lines; i++){  // Output gradient Y
        for(int j = 0; j < cols; j++){
           cout << grad_y[i][j] << " ";
        }
        cout << "\n";
    }

    return grad_y;
}

vector<vector<double> > matrixNorm(vector<vector<double> > matrix) {

    vector<vector<double> > matrix_norm( cols , vector<double>( lines ) );
    double max = 0;
    int max_col,max_line;

    for(int i = 0; i < lines; i++){  // Encontra o máximo da matrix
        for(int j = 0; j < cols; j++){

           if(matrix[i][j] > max){
               max = matrix[i][j];
               max_col = j;
               max_line = i;
           }

        }
    }
    cout << "Max value " << max << " at line " << max_line+1 << ", col " << max_col+1 <<  endl;

    for(int i = 0; i < lines; i++){  // Normaliza matrix
        for(int j = 0; j < cols; j++){

            matrix_norm[i][j] = matrix[i][j]/max;

        }
    }
    cout << "Matrix normalizada" << endl;

    for(int i = 0; i < lines; i++){  // Output gradient Y
        for(int j = 0; j < cols; j++){
           cout << matrix_norm[i][j] << " ";
        }
        cout << "\n";
    }

    return matrix_norm;
}

class vectors {
public:
    double module, phase;
};

vector<vector<double> > matrixModPhase(
                                        vector<vector<double> > grad_x,
                                        vector<vector<double> > grad_y
                                      ) {

    vectors matrix_mod_phase[lines][cols]; // matrix de modulos e phases

    for(int i = 0; i < lines; i++){
        for(int j = 0; j < cols; j++){
          matrix_mod_phase[i][j].module = 1.0;
          matrix_mod_phase[i][j].phase = 1.0;
        }
        cout << "\n";
    }


    vector<vector<double> > matrix_mp;
    return matrix_mp;
}

int main(int argc, char** argv) {
    
    
    string filename;
    filename = "/home/cristiano/matrix4.dat";
    ifstream iFile;
    iFile.open(filename.c_str());

    if(!iFile) { // file couldn't be opened
        cerr << "Error: file could not be opened" << endl;
        exit(1);
    }

    cols = matrix_cols(iFile); // Get matrix size
    lines = matrix_lines(iFile);
    cout << "Matrix " << cols << "x" << lines << " - " << filename << endl;

    vector<vector<double> > matrix;

    matrix = matrixRead(iFile); // Load matrix

    vector<vector<double> > matrix_norm;

    matrix_norm = matrixNorm(matrix); // Normaliza matrix

    vector<vector<double> > grad_x,grad_y;

    grad_x = gradX(matrix_norm); // Calc gradient
    grad_y = gradY(matrix_norm); // Calc gradient

    vector<vector<double> > matrix_mod_phase;

    matrix_mod_phase =  matrixModPhase(grad_x, grad_y); // Calc Modulo e fase

    iFile.close();

    return (EXIT_SUCCESS);
}

Last edited on
Topic archived. No new replies allowed.