How Do You Create a Sub-Matrix Based on Vector Conditions?

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Logic Program
Click For Summary
The discussion revolves around extracting a sub-matrix R from a larger square matrix M based on the values in a column vector C. The entries in C determine which rows and columns of M to keep, where a value of 0 in C indicates that the corresponding row and column should be excluded. The goal is to implement a method to populate R with the remaining entries from M after crossing out the specified rows and columns.The proposed solution involves using nested loops to iterate through the indices of M. If the corresponding entry in C is 1, the code assigns the value from M to R, while maintaining separate counters (rx and ry) to track the position in R. There is also a clarification regarding array indexing, noting the difference between 0-based and 1-based indexing, which affects how the loops are structured. The final pseudocode reflects these adjustments to ensure correct indexing and assignment of values.
Saladsamurai
Messages
3,009
Reaction score
7
I am confusing the heck out of myself here.

This is what I have

Square matrix that is M = 'm x m'
A column vector that is C = 'm x 1'
a smaller square matrix that is R ='r x r'Here is what I am trying to accomplish; I usually do this by hand:

The entries of the column vector C are either 1 or 0. If the ithentry in C is zero then I can "cross out" the ith row AND column in M. For example, if M is 5 x 5 and the 2nd and 4th entries of C are 0, I draw a line through the 2nd and 4th row AND column of M.

This leaves me with a 3 x 3 sub-matrix that is made up of the un-crossed out entries of M.

This sub-matrix is R. So I need to find a way to assign these values to R from M after crossing out rows and columns according to the entries of C.I have already created a test to initialize a matrix R of the correct dimensions; it is now a matter of getting the entries in there.

Something like:

Code:
For i = 1 to M
    For j = 1 to M
    If C(i,1) = 1 Then {R[something,something] = M[Something,Something]

     Next j
Next i
But I think I will need 2 more counter variables in addition to i and j so that if C(i,1)=0 the indices of R do not increment.

I am just a little lost now. Can I get a little help here? :smile:
 
Technology news on Phys.org
I would do something along the lines of:

(Pseudocode)
Code:
rx = 0
ry = 0
for(mx = 1 to M) {
    if(C[mx] == 1) {
        ry = 0
        for(my = 1 to M) {
            if(C[my] == 1) {
                R[rx][ry] = M[mx][my]
                ry++
            }
        }
        rx++
    }
}

DaveE
 
davee123 said:
I would do something along the lines of:

(Pseudocode)
Code:
rx = 0
ry = 0
for(mx = 1 to M) {
    [B]if(C[mx] == 1) {
        ry = 0[/B]
        for(my = 1 to M) {
            if(C[my] == 1) {
                R[rx][ry] = M[mx][my]
                ry++
            }
        }
        rx++
    }
}

DaveE


Hey there Dave :smile: I am kind of bad at this...what is the part on bold doing?
 
It's saying if the value at C[mx] (the mxth value in the single-dimensional matrix C) is equal to 1, then execute the code that's in the following set of braces. The first line in that sub-block of code says to set ry equal to 0.

I should note that actually, there's a little confusion there in terms of indexing. If your arrays are 0-based, then you want:

Code:
rx = 0
ry = 0
for(mx = 0 to M-1) {
    if(C[mx] == 1) {
        ry = 0
        for(my = 0 to M-1) {
            if(C[my] == 1) {
                R[rx][ry] = M[mx][my]
                ry++
            }
        }
        rx++
    }
}

And if your arrays are 1-based, then you want:

Code:
rx = 1
ry = 1
for(mx = 1 to M) {
    if(C[mx] == 1) {
        ry = 1
        for(my = 1 to M) {
            if(C[my] == 1) {
                R[rx][ry] = M[mx][my]
                ry++
            }
        }
        rx++
    }
}

DaveE
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
3
Views
2K
Replies
31
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K