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

In summary: Actually, I think you are trying to do something like this: (Pseudocode)rx = 0ry = 0for(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]
  • #1
Saladsamurai
3,020
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
  • #2
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
 
  • #3
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?
 
  • #4
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
 
  • #5


I can understand how confusing this task may seem. Let me break it down for you.

Firstly, it is important to understand the purpose of the program. It seems like you are trying to create a sub-matrix R from a larger matrix M, based on the entries of a column vector C. The column vector C contains only 1s and 0s, and if the ith entry in C is 0, then the ith row and column in M should be "crossed out". This means that the values in the corresponding row and column should not be included in the sub-matrix R.

To achieve this, you will need to use a nested loop. The outer loop will go through each row of M (i.e. for i = 1 to M) and the inner loop will go through each column of M (i.e. for j = 1 to M). Within this loop, you will need to check the value of C(i,1). If it is 1, then the value in M(i,j) should be included in R. So, you can assign it to R using R[i',j'] = M[i,j], where i' and j' are the indices for R. These indices can simply be the values of i and j, since you are only including values from the un-crossed out rows and columns.

However, if C(i,1) is 0, then you do not want to include the value in M(i,j) in R. In this case, you can simply skip over the assignment step and move on to the next iteration of the inner loop.

So, your code may look something like this:

For i = 1 to M
For j = 1 to M
If C(i,1) = 1 Then
R[i,j] = M[i,j]
End If
Next j
Next i

This way, you will only include the values from the un-crossed out rows and columns in R.

I hope this helps to clarify the logic of your program. Good luck!
 

1. What is the importance of logic in programming?

Logic is essential in programming because it allows us to create programs that are logical and efficient. It helps us break down complex problems into smaller, more manageable steps, making it easier to write code that produces the desired results. Additionally, having a logical approach to programming helps in identifying and fixing errors in the code.

2. How can I improve my logic skills for programming?

One way to improve logic skills for programming is to practice solving logic puzzles and problems. These exercises can help develop critical thinking and problem-solving abilities, which are crucial for writing efficient programs. Additionally, learning different programming languages and their syntax can also help improve logic skills.

3. Can logic errors be more challenging to fix than syntax errors?

Yes, logic errors can be more challenging to fix than syntax errors. While syntax errors are easily identified by the compiler, logic errors can be harder to detect because they do not cause the program to crash. They can cause unexpected or incorrect results, making it challenging to pinpoint the exact source of the error.

4. Is it necessary to have a strong understanding of logic to be a good programmer?

Having a strong understanding of logic is crucial to becoming a good programmer. Programming involves creating a set of instructions that the computer can follow to solve a problem. Without a logical approach, it is challenging to write code that produces the desired results and is efficient. Therefore, a good understanding of logic is essential for writing high-quality programs.

5. How can I debug logic errors in my code?

Debugging logic errors involves a systematic approach to identify and fix the issue. It is helpful to use debugging tools provided by the programming language, such as a debugger, to track the flow of the program and identify where the error occurs. Additionally, reviewing the code and checking for any assumptions or mistakes can also help in debugging logic errors.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Linear and Abstract Algebra
Replies
4
Views
868
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
Replies
4
Views
1K
  • Programming and Computer Science
Replies
15
Views
3K
  • Precalculus Mathematics Homework Help
Replies
1
Views
519
Back
Top