1

Matrix representation. image 2. i have written like this for horizontal group of -1. but i am not sure is it correct or not? enter image description here

i need a Generalized mathematical equation for a group of horizontal -1's and group of vertical -1's in a matrix. if there are groups of -1's in a row and if there is a group of 1's in column. attached figure highlighted red color elements are the required groups.

Ali
  • 3
  • 4
  • where is the attached image ? – nits.kk Aug 14 '18 at 10:18
  • @nits.kk . i have attached it now. infact i need group of horizontal -1's and vertical -1's entries. – Ali Aug 14 '18 at 10:23
  • Your question is not clear. By "equation" do you mean a relation, a function that generates those ones from the row and column numbers, or something else? Your text mentions `1's` but your graphic has `-1`s (except for one `1` at row 6 column 13). Do you want an "equation" for each group of ones, for both groups together, or for the entire matrix? And so on. Please clarify. A simple example, with its desired answer, would help. Finally, what attempts have you made on this problem, and just where are you stuck? – Rory Daulton Aug 14 '18 at 10:42
  • @ Rory Daulton yes i need a mathematical equation to generate ,1) for where ever there is a group of horizontal -1's in matrix , 2) another equation for vertical -1's in the matrix. e.g there is a horizontal group of -1's at a511=-1 and a512=-1. And there is another vertical group of -1's i.e. v714=-1, v814=-1, v914=-1, v1014=-1. – Ali Aug 14 '18 at 10:58
  • I'm voting to close this question as off-topic because it is not about programming. OP explicitly asks for an equation, not code. – High Performance Mark Aug 15 '18 at 07:59

2 Answers2

1

Edit:

After talking with OP in the comments, it sounds like he might want a way to have a computer go through and search for contiguous blocks of -1's in rows or columns of a matrix. I'm providing an example here where you can search through the rows, using numpy in python:

import numpy as np;

V = np.array([
    [0,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,-1,0,0,0,0,-1,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,-1,-1,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,-1,-1,1,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,-1,-1,0,0,0,-1,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])

def group_finder(A, num):
    print("Searching for groups of %d's in the rows of this matrix:"%num)
    print(A)
    print

    m = M.shape[0]
    n = M.shape[1]

    #find groups in rows
    for i in range(m):

        last_matched = False;
        index_of_group_start = None;

        #go through the entries in each row
        for j in range(n) :

            this_matched = (M[i][j] == num)

            if(this_matched):

                if(last_matched):
                    continue #keep processing the matching numbers

                else:
                    index_of_group_start = j

            else:
                if(last_matched and index_of_group_start+1 != j):

                    print("There is a group of %d's in row %d."
                            %(num, i+1))#add one because numpy is zero-indexed.

                    print("It starts at column %d and ends at column %d"
                                %(index_of_group_start+1, j))
                    print

            #update values for next iteration
            last_matched = this_matched


group_finder(V, -1)

When I run that code on my machine, this is what I get as output:

Searching for groups of -1's in the rows of this matrix:
[[ 0 -1 -1 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0 -1  0  0  0  0 -1  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0 -1 -1  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0 -1 -1  1  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0 -1 -1  0  0  0 -1  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0 -1 -1  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0 -1  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0 -1  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0 -1  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 -1  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 -1  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 -1]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 -1]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 -1]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]]

There is a group of -1's in row 1.
It starts at column 2 and ends at column 4

There is a group of -1's in row 3.
It starts at column 6 and ends at column 7

There is a group of -1's in row 4.
It starts at column 8 and ends at column 9

There is a group of -1's in row 5.
It starts at column 11 and ends at column 12

There is a group of -1's in row 7.
It starts at column 13 and ends at column 14

Old Answer:

I think you are having a hard time describing what you are asking for. Understandable. I think I can help you describe your problem.

It seems like you need to find consecutive cells of -1's in a spreadsheet program, like Microsoft Excel or LibreOffice. You want something that can find a contiguous group of negative ones in a matrix, and then operate on them. You're looking for something like the COUNTIF function, but instead of counting cells with a specific property, you are trying to highlight or select cells with a specific property. Specifically, you want to highlight cells that contain a -1, and have a non-diagonal neighboring cell that also contains a -1. Your question is how you can make your spreadsheet program do that.

If that is the case, you have used the wrong tags for your question. Just because someone knows math does not mean that they know spreadsheet software. I would recommend changing the tags on your question. Some good tags might be calc-libreoffice, excel, or spreadsheet. In fact, there is an answer to a similar question with the excel tag, though it does not deal with rows.

There's a chance you are instead trying to understand how a mathematician would describe a contiguous group of negative ones in a matrix. In that case, I think your question might be better phrased as "What mathematical expression should I use to describe a submatrix that consists entirely of -1s and is restricted to a portion of only one row or a portion of only one column."

One answer to that question is to use a form of matrix subscript notation. I would notate your first group as V5, 11 -- 12 and your second group as V7 -- 10, 15. I would then just say that the entries in V5, 11 -- 12 are all -1.

Logan Schelly
  • 331
  • 2
  • 11
  • "What mathematical expression should I use to describe a submatrix that consists entirely of -1s and is restricted to a portion of only one row or a portion of only one column." This is exactly what i want to ask. I am working on matrices and want to find out a function/equation for my problem. If you could please help me. Thanks – Ali Aug 15 '18 at 02:29
  • This might be a good question for the the [mathematics stack exchange](https://math.stackexchange.com/). In fact, I found a nice formula [there](https://math.stackexchange.com/questions/1322942/formula-to-obtain-a-submatrix). It looks like you could to take two 18x18 identity submatrices, put them on both sides of your matrix V, and then delete the rows you don't want from the identity matrix on the left, and then delete the columns you don't want from the identity matrix on the right. – Logan Schelly Aug 15 '18 at 02:48
  • I am really finding it hard. Infact, i need a mathematical function that how can i obtain a desired sub matrix contains only -1 horizontally and another sub matrix contains -1 vertically. – Ali Aug 15 '18 at 03:24
  • Do you need a way to _find_ the submatrix, or do you need a way to _get_ the submatrix? – Logan Schelly Aug 15 '18 at 03:47
  • Did you see how they got a desired submatrix in the problem at this page [https://math.stackexchange.com/questions/1322942/formula-to-obtain-a-submatrix](https://math.stackexchange.com/questions/1322942/formula-to-obtain-a-submatrix)? How was that answer different from the answer that you need? – Logan Schelly Aug 15 '18 at 03:56
  • I need a formula for getting group of horizontal -1 and group of vertical -1 in my matrix. as you can there is one horizontal group at the first row (v2,v3,v4). and there is a vertical group of -1 at row v7, v8,v9,10. – Ali Aug 15 '18 at 04:04
  • i am working on to find out an algorithm that how can i search a matrix for group of horizontal -1 and group of vertical -1. To do this, i need a mathematical formula to obtain this kind of groups within a matrix. – Ali Aug 15 '18 at 04:09
  • I do not know how to construct a _formula_ but I could construct an _algorithm_ (or a _computer program_) that could return a list of ALL submatrices that contain exclusively -1. Looks like some guys were doing something similar in numpy at this page: [https://stackoverflow.com/a/26898043/10227068](https://stackoverflow.com/a/26898043/10227068) – Logan Schelly Aug 15 '18 at 04:38
  • I would really appreciate if you could construct that could return a list of ALL submatrices that contain exclusively -1. Thank you – Ali Aug 15 '18 at 04:44
  • I won't be able to right now, because I am on my mobile phone and ready to go to bed. I can give you one tomorrow. In the meantime, I suggest you learn more about python and the numpy library. Here's the source that I learned numpy from: [http://www.acme.byu.edu/wp-content/uploads/2016/08/NumpyIntro.pdf](http://www.acme.byu.edu/wp-content/uploads/2016/08/NumpyIntro.pdf) – Logan Schelly Aug 15 '18 at 04:59
  • I've edited my answer. I've provided an example of how to search for rows of matching numbers in a matrix using the numpy library in python. – Logan Schelly Aug 15 '18 at 16:46
0
array = {
   {0,-1,-1,-1, 0, 0, 0, 0, 0,-1,-1, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0,-1, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0,-1,-1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1, 0, 0, 0,-1, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};

A couple of extra -1s were added to the first row to demonstrate the code's ability to find multiple spans of -1s.

Function definitions

fromTo[y_] := #[[{1, -1}]] & /@ SplitBy[MapIndexed[
     If[# == -1, Last[#2]] &, y], Head] /. {a_, a_} -> Nothing

wherearethey[a_, dir_] := Grid[Prepend[DeleteCases[MapIndexed[
     {Last[#2], fromTo[#1]} &, If[dir == "Row", a, Transpose[a]]],
    {__, {}}], {dir, "{{From, To}}"}], Alignment -> Left]

Horizontal scan

wherearethey[array, "Row"]
Row  {{From, To}}
1    {{2,4},{10,11}}
3    {{6,7}}
4    {{8,9}}
5    {{11,12}}
7    {{13,14}}

So in the first line there are -1s from position 2 to 4 and from 10 to 11.

Vertical scan

wherearethey[array, "Column"]
Column  {{From, To}}
10      {{1,2}}
14      {{7,10}}
17      {{13,14}}
18      {{15,17}}
Chris Degnen
  • 8,443
  • 2
  • 23
  • 40