Solved

Diagonal elements

Posted on 2002-06-14
8
239 Views
Last Modified: 2010-04-02
Hi,

I have to set all the diagonal elements of a 2D matrix to a given value (i.e. zero). This is easy if the matrix is square, but I'm having problems with non-square matrices.

This is what I've tried so far:

int N , M; //Number of rows and columns

float* fData = new float [N * M];

float fValue = 0.0f;




float fSlope  = (float)M / (float)N;



for (register int i = 0; i < M; i++)
{
     
     
     int j = (int) floor( i / fSlope + 0.5);
     
     *fData = fValue;
     
     int nOffset = (int)floor (nCols * slope + 0.5) +1;
     
     fData += nOffset;
     
}

This works fine if the matrix is square but fails in the non-square case

Thanks for your help,
elito
0
Comment
Question by:elito
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7077882
Please make clear How the selection criteria

Actually, Only square matrix have only diagonal elements.

If you want this is a particular application, so you have a criteria right...

Please mention that...

Roshmon
0
 
LVL 86

Expert Comment

by:jkr
ID: 7078022
As you are allocating the matrix as a linear array, you could just

int N , M; //Number of rows and columns
int size = N * M;
float* fData = new float [size];

float fValue = 0.0f;

for (register int i = 0; i < size; i++) {

 *(fData + i) = fValue;
}

0
 
LVL 1

Expert Comment

by:Pavlik
ID: 7079463
Explain for example what do you want to have in case of 3x4 matrix.

Something like this?
#000
0##0
000#
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:LoungeLizard
ID: 7079593
The problem is that a non-square matrix, by definition, does not have a diagonal. The best you can do is an approximation of the diagonal. So first decide how you would estimate this diagonal, let us know, and then we can help you with the code.

Just a question (out of curiosity): Why do you need to do that? I am not aware of any matrix operations/calculations that need that for
0
 
LVL 4

Accepted Solution

by:
jos010697 earned 200 total points
ID: 7080307
The projection of two coordinate (x,y) onto a one dimensional array f[N*M] is easy: f[M*y+x]. Your
'diagonal' traversal is equivalent to a raster graphics
line drawing problem, i.e. which pixels to choose when
there is no exact match. Have a look at the Bresenham
line drawing algorithm (google is your friend here).

kind regards
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7082159
It seems to me that there are many diagonals:
    #00000
    0#0000
    00#000
and
    0#0000
    00#000
    000#00
and
    00#000
    000#00
    0000#0
and
    000#00
    0000#0
    00000#
But I am one of few people I know who enjoy playing tick-tack-toe in a rectagular grid, so please shoot me.

-- Dan

0
 

Author Comment

by:elito
ID: 7082160
sorry for the late response. I tried Bresenham algorithm and yes, that was exactly what I needed.
Thanks for the help,
elito
0
 

Author Comment

by:elito
ID: 7082168
Dan,
I meant the main diagonal of the matrix, the one that connects point (0,0) with point (M, N).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 82
C++ question 3 80
C++ mouse_event mouse look 7 128
COM server issue 2 11
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question