473,698 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What does this matrix code do?

Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:

#include <stdio.h>

int main()

{

int check_mat[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 ,
9 } };
int im,jm;

/* Print starting values to screen */

for ( im = 0 ; im < 3 ; im++ )

{

for( jm = 0 ; jm < 3 ; jm++ )

{

printf("%d\t" , check_mat[im][jm] );

}

printf("\n");

}

/* Perform some kind of matrix manipulation */

for( im = 0 ; im <= 2 ; im++ )

{

for( jm = 0 ; jm <= im ; jm++ )

{

check_mat[jm][im] = check_mat[im][jm];

}

}

printf("\n");

/* Print final manipulated values to screen */

for ( im = 0 ; im < 3 ; im++ )

{

for( jm = 0 ; jm < 3 ; jm++ )

{

printf("%d\t" , check_mat[im][jm] );

}

printf("\n");

}

return 0;

}
Kind Regards,

Matt

Aug 22 '07 #1
8 2522
Matt <ma*****@hotmai l.comwrites:
Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:
I am not sure what *kind* of answer you want. The program shows you,
at some level, exactly what is done to the matrix.

Other related questions such as "what is this transformation called?"
and "why would one do it?" are not C questions and you'll get fuller
answers elsewhere (though I can't point to a good group).

<code snipped>

--
Ben.
Aug 22 '07 #2
Matt <ma*****@hotmai l.comwrote:
Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:
#include <stdio.h>
int main()
{
int check_mat[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } };
int im,jm;
/* Print starting values to screen */
for ( im = 0 ; im < 3 ; im++ )
{
for( jm = 0 ; jm < 3 ; jm++ )
{
printf("%d\t" , check_mat[im][jm] );
}
printf("\n");
}
/* Perform some kind of matrix manipulation */
for( im = 0 ; im <= 2 ; im++ )
{
for( jm = 0 ; jm <= im ; jm++ )
{
check_mat[jm][im] = check_mat[im][jm];
}
}
Actually

for ( im = 1; im < 3; im++ )
for ( jm = 0; jm < im; jm++ )
check_mat[ jm ][ im ] = check_mat[ im ][ jm ];

should be enough, saving a few operations.
printf("\n");
/* Print final manipulated values to screen */
for ( im = 0 ; im < 3 ; im++ )
{
for( jm = 0 ; jm < 3 ; jm++ )
{
printf("%d\t" , check_mat[im][jm] );
}
printf("\n");
}
return 0;
}
Well, you seem to have a working program here, so you can compare
the matrix before the operation and afterwards. That should tell
you immediately what operation is done on the matrix. If you still
don't see it get out your book about linear algebra and read what
the "transpose" of a matrix is.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Aug 23 '07 #3
jt@toerring.de (Jens Thoms Toerring) writes:
Matt <ma*****@hotmai l.comwrote:
>Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:
[...]
Well, you seem to have a working program here, so you can compare
the matrix before the operation and afterwards. That should tell
you immediately what operation is done on the matrix. If you still
don't see it get out your book about linear algebra and read what
the "transpose" of a matrix is.
The posted code doesn't transpose the matrix, though I can believe
(barely) that it was intended to.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 23 '07 #4
Ben Bacarisse wrote:
Matt <ma*****@hotmai l.comwrites:
>Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:

I am not sure what *kind* of answer you want. The program shows you,
at some level, exactly what is done to the matrix.

Other related questions such as "what is this transformation called?"
and "why would one do it?" are not C questions and you'll get fuller
answers elsewhere (though I can't point to a good group).

<code snipped>
But when you do ask elsewhere, dispense with the double spacing of
the code portion.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 23 '07 #5
In article <5j************ *@mid.uni-berlin.dejt@toerring.de (Jens Thoms Toerring) writes:
Matt <ma*****@hotmai l.comwrote:
....
for( im = 0 ; im <= 2 ; im++ )
{
for( jm = 0 ; jm <= im ; jm++ )
{
check_mat[jm][im] = check_mat[im][jm];
}
}

Actually
for ( im = 1; im < 3; im++ )
for ( jm = 0; jm < im; jm++ )
check_mat[ jm ][ im ] = check_mat[ im ][ jm ];
should be enough, saving a few operations.

Well, you seem to have a working program here, so you can compare
the matrix before the operation and afterwards. That should tell
you immediately what operation is done on the matrix. If you still
don't see it get out your book about linear algebra and read what
the "transpose" of a matrix is.
Only, it is not the transpose. When doing a transpose you interchange
elements.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 23 '07 #6
Matt wrote:
Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:
(code snipped)

When presented with a problem like this, I try to visualize the
operations. You already have one handy tool: the program prints the
matrix before and after. Run it and study the output. See if you can
discern the general pattern.

When I looked at your code, I used another tool: playing computer. I
played computer by writing down the values of the matrix on a piece of
paper in the standard row by column arrangement. As I mentally executed
each statement, I drew arrows to show how the data was moved (usually I
scratch out old values and write in the new ones, but arrows worked fine
for your code). When I was finished, I could clearly see the data
pattern illustrating the transformation.

Playing computer is a powerful tool for me when I am working with
algorithms that shuffle data. Examples of code that I have developed
using this are
1) implementing a data heap structure
2) rotating an array of values by a given number of positions, moving
each item directly to its destination.
3) tree construction and balancing operations.

Try it!

--
Thad
Aug 23 '07 #7
Matt wrote:
Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:
/* Perform some kind of matrix manipulation */

for( im = 0 ; im <= 2 ; im++ ) {
for( jm = 0 ; jm <= im ; jm++ ){
check_mat[jm][im] = check_mat[im][jm];
}
}
This is slightly off topic, and it won't hurt anything I suppose,
but when the inner loop reaches jm==im all it does is read a value from
one cell of that array and then write it back into the same cell.
Assuming the compiler doesn't optimize this away, it's a wasted operation.
Regards,

David Mathog
Aug 23 '07 #8
On Wed, 22 Aug 2007 15:12:47 -0700, Matt <ma*****@hotmai l.comwrote:
>Hello. I'm struggling to work out what bit of matrix manipulation the
following piece of code is doing to the 3x3 matrix given:

#include <stdio.h>

int main()

{
snip double spaced code
>
}
Why not just execute it and see for yourself.
Remove del for email
Aug 26 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19036
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
15
2147
by: greenflame | last post by:
First of all I only have IE for testing. Ok. I have a script that is supposed to show, not evaluate, the indefinite integral of something. When I run the script it just craches IE. I have tried to get all the obvious bugs out... Um... The script is at: http://ali.freezope.org/idf test and what it is supposed to show is at: http://ali.freezope.org/idf result
11
2593
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
1
7964
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
3
1288
by: kirknew2SQL | last post by:
I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. 1. initTreeView(TreeNode N) creates a new “temp” table to hold the children for each node. Each time the table is created it has the same name “temp”. Why doesn’t the table just get over written each time? 2. In the foreach (DataRow r3 in temp.Rows) loop, if the temp table is empty like when Menu11 is reached the...
3
293
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code works. 1. initTreeView(TreeNode N) creates a new "temp" table to hold the children for each node. Each time the table is created it has the same name "temp". Why doesn't the table just get over written each time? 2. In the foreach (DataRow r3...
1
1439
by: tiddwaylll | last post by:
I once had a matrix working like this matrix So, since its indexed with 0, I used them as matrix or matrix. Then the other day, I forgot the 0 index, and used matrix. And it worked perfectly fine! And worse so, the program didn't work anymore when I revert that back to matrix. Also matrix now had ridiculous values inside.
12
39165
by: slizorn | last post by:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int err how do i fix that error? for example given to this section of code.. Matrix::addMatrix(Matrix m1, Matrix m3) { for(int a = 0; a < row; a++) { for(int b = 0; b < col; b++)
2
2661
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class matrix{ public: matrix();
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.