An n-by-n square logical matrix can be represented by a cell vector of n element
ID: 3847142 • Letter: A
Question
An n-by-n square logical matrix can be represented by a cell vector of n elements where the kth
element corresponds to the kth row of the matrix. Each element of the cell vector is a row
vector of positive integers in increasing order representing the column indexes of the logical
true values in the given row of the matrix. All other elements in the given row of the logical
matrix are false. Write a function called logiunpack that takes such a cell vector as its
only input and returns the corresponding square logical matrix. For example, such a cell vector
representation of a 100-by-100 logical matrix with the only true elements at indexes (10,20)
and (10,75) would have only one non-empty element of the cell vector at index 10. That
element is the vector [20 75].
Explanation / Answer
I have chosen C++ to code in, as programming language was not specified.
I have also assumed 0 based indexing i.e., each cell vector contains values <= n-1.
Return type of logiunpack function is Vector of boolean vectors, where each cell vector contains n elements and can be indexed from 0 to n-1.
I have added appropriate comments in code for easy understanding.
A print_2d_bool_vector function is also added so you can see what is returned by logiunpack.
In main function, an example of input output is shown.
Example:
logiunpack input = { {0,1}, {}, {} }
output = { {true, true, false}, {false, false, false}, {false, false, false} }
Note that n is decided by the number of vectors in input. In above example there are 3 vectors inside input last 2 are empty and first one contains 0 and 1.
C++ Code:
#include <iostream>
#include <vector>
using namespace std;
//Input - Vector of Integer Vectors, as specified in question
//Return type - Vector of boolean vectors, size of each individual vector is equal to n
//Assumes 0-based indexing
vector< vector<bool> > logiunpack(vector< vector<int> > V){
//This n is same as of question
//output will be n-by-n
int n = V.size();
vector< vector<bool> > return_vector;
//loop through all the elements of vector
for(int i=0; i<n; i++){
//get ith row, containing indices of true values
int size_of_cell_vector_i = V[i].size();
//index variable will go from 0 to n-1 so there will be n values in ith row
int index = 0;
//row_i will be storing actual true false values corresponding to ith row
vector<bool> row_i;
for(int j=0; j<size_of_cell_vector_i; j++){
//true_index is the index of true values
int true_index = V[i][j];
//we fill all the row_i vector with false values until the index reaches true index
while(index<true_index){
row_i.push_back(false);
index++;
}
//store true in row_i, at this point index=true_index
row_i.push_back(true);
index++;
}
//fill rest of row_i vector with false values
while(index<n){
row_i.push_back(false);
index++;
}
//push row_i vector to return_vector
return_vector.push_back(row_i);
}
return return_vector;
}
//For printing the 2d vector returned by logiunpack function
void print_2d_bool_vector(vector< vector<bool> > V){
int n = V.size();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << V[i][j] << " ";
}
cout << endl;
}
}
int main(){
vector<int> v1,v2;
v1.push_back(0);
v1.push_back(1);
vector< vector<int> > V;
V.push_back(v1);
V.push_back(v2);
V.push_back(v2);
print_2d_bool_vector(logiunpack(V));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.