1D vector(matrix) with one row and 7 column and in line: result += (matrix * decode[b])
Would be nice, but no. If say matrix is a vector<int> and decode[b] is an int, then what do you expect the result of matrix * decode[b] to be? Do you want each element of matrix to be multiplied by decode[b] or ...
Mathematically, what you mean by matrix * decode[b] ?
Do you mean that Matrix refers to the name of the routine in the first code? If so, then you need to call Matrix() as a function in Decode() eg
Then you have the similar mathematical question. What do you mean mathematically by matres * decode[b]? In this case matres is a vector of a vector.
However, a better mathematical question is given
vector<vector<int>> matres and int decode[7]
What is meant mathematically by matres * decode?
Once that is known, then it can be coded.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
std::cout << "Decode" << std::endl;
int decode[7];
for (int bits = 0; bits < 7; bits++) {
if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
else { decode[bits] = 1; }
}
std::vector<std::vector<int>> matres(nbits);
Matrix(matres, nbits);
char synd[3];
for (int a = 0; a < 3; a++) {
char result = 0;
for (int b = 0; b < 7; b++) {
result += (matres[a] * decode[b]);
}
synd[a] = result % 2;
}
}
In this case, the mathematical question is still what is meant by a vector<int> * int? Is each element of the vector multiplied by the it value?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
#include <vector>
#include <limits>
#include <algorithm>
#include <iostream>
#include <numeric>
constexpr size_t nbits = 3 % std::numeric_limits<unsigned long long>::digits;
std::vector<int>& operator*(std::vector<int>& matres, int val)
{
for (auto& m : matres)
m *= val;
return matres;
}
int operator+=(int val, std::vector<int>& matres)
{
return std::accumulate(matres.begin(), matres.end(), val);
}
std::ostream& operator<<(std::ostream& os, std::vector<std::vector<int>> matres)
{
for (const auto& row : matres)
{
for (const auto& v : row)
os << v << ' ';
os << '\n';
}
return os;
}
void Matrix(std::vector< std::vector<int> > &result, size_t nbits)
{
result.resize(nbits);
for (unsigned long long number = 1, n = number, ub = 2 << (nbits - 1); number < ub; n = ++number) {
for (auto& vec : result)
{
vec.push_back(n % 2), n >>= 1;
}
}
std::reverse(std::begin(result), std::end(result));
std::cout << "matrix is: " << std::endl << result << std::endl;
}
int voltage = 7;
void Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
std::cout << "Decode" << std::endl;
int decode[7];
for (int bits = 0; bits < 7; bits++) {
if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; } else { decode[bits] = 1; }
}
char synd[3];
std::vector<std::vector<int>> matres(nbits);
Matrix(matres, nbits);
for (int a = 0; a < 3; a++) {
char result = 0;
for (int b = 0; b < 7; b++) {
result += (matres[a] * decode[b]);
}
synd[a] = result % 2;
}
}
Last edited by 2kaud; April 23rd, 2018 at 11:32 AM.
Reason: Added operator<<
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
for a multiplication of 2 matrices if the bits of the first matrix(1D matrix with one row and 7 column) and the bits of each row of second matrix(which is the matrix correspond to the first code(with 3 row and 7 column) multiply together and then append to next , I will reach my proper solution,
in the following, I attached an image contain an example that I wrote by hand
Ok. I understand. I'll knock some code up for this tomorrow (GMT)).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Hello
Dear @2kuad, your code works, but unfortunately I cannot use it in my code, because the decode vector generate randomly in another part of my code.
the structure of the code must follow the code which I attached in #1 (second code).
you posted a code in #3 like the following:
Code:
void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
std::cout << "Decode" << std::endl;
int decode[7];
for (int bits = 0; bits < 7; bits++) {
if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
else { decode[bits] = 1; }
}
std::vector<std::vector<int>> matres(nbits);
Matrix(matres, nbits);
char synd[3];
for (int a = 0; a < 3; a++) {
char result = 0;
for (int b = 0; b < 7; b++) {
result += (matres[a] * decode[b]);
}
synd[a] = result % 2;
}
}
In this code, if instead of [[[result += (matres[a] * decode[b]);]]] I replace [[[result += (matres[a] [b] * decode[b]);]]] then every thing is okay, but could you please explain me the lines which you added to this piece of code(the following lines are added by you):
I want to be sure about the logic of this lines:
defines matres as a vector of vector of int with an initial number of elements of nbits for the outer vector.
Code:
Matrix(matres, nbits);
Call the function Matrix to generate the contents of the variable matres. nbits is also passed as a parameter. However, if matres is defined as above then this parameter is not needed as matres is sized before the function call as opposed to within the function call. How matres is resized depends somewhat on requirements.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
could you please explain which element or variable must be called from the first matrix in first code for multiplying with the 1D matrix in second code??
After this, matres is the 2d matrix.
decode is the 1d matrix.
synd is the required resultant matrix.
From second code in post #1 consider (not tried)
Code:
void Ham::Decode(std::vector<double> &codedVector, std::vector<int> &decodedVector) {
std::cout << "Decode" << std::endl;
int decode[7];
for (int bits = 0; bits < 7; bits++) {
if (codedVector[bits] < (voltage / 4)) { decode[bits] = 0; }
else { decode[bits] = 1; }
}
std::vector<std::vector<int>> matres(nbits);
Matrix(matres, nbits); //Call Matrix in first code
char synd[3];
for (int a = 0; a < 3; a++) {
char result = 0;
for (int b = 0; b < 7; b++) {
result += (matres[a][b] * decode[b]);
}
synd[a] = result % 2;
}
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.