答案 0 :(得分:1)
在评论中与OP交谈后,听起来他可能想要让计算机通过并搜索矩阵的行或列中-1的连续块的方法。我在这里提供一个示例,您可以在python中使用numpy来搜索行:
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)
当我在计算机上运行该代码时,这就是我得到的输出:
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
我认为您很难描述您的要求。可以理解的我想我可以帮助您描述您的问题。
似乎您需要在电子表格程序(例如Microsoft Excel或LibreOffice)中找到-1的连续单元格。您需要可以在矩阵中找到一组连续的负数,然后对其进行操作的东西。您正在寻找类似COUNTIF
函数的方法,但不是要 counting 具有特定属性的单元格,而是要尝试 highlight 或 select < / em>具有特定属性的单元格。具体来说,您要突出显示包含-1的单元格,并具有也包含-1的非对角相邻单元格。您的问题是如何使电子表格程序做到这一点。
在这种情况下,您为问题使用了错误的标签。仅仅因为有人知道数学并不意味着他们知道电子表格软件。我建议更改您问题上的标签。一些不错的标签可能是calc-libreoffice
,excel
或spreadsheet
。实际上,similar question with the excel
tag有一个答案,尽管它不处理行。
您有机会尝试理解数学家如何在矩阵中描述一组负数。在那种情况下,我认为您的问题最好用“我应该使用什么数学的表达式来描述一个完全由-1组成且限于一行或一部分的子矩阵?只有一列。”
该问题的一个答案是使用一种矩阵下标表示法。我会将您的第一组标记为V 5-11-12 ,将您的第二组标记为V 7-10、15 。然后我只想说V 5,11-12 中的条目都是-1。
答案 1 :(得分:0)
<?php
print_r($_POST["innerArray"]);
?>
在第一行中添加了两个额外的<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.3.1.min.js"></script>
<script src="scripts.js"></script>
</head>
<body>
</body>
</html>
,以演示代码能够找到多个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}};
跨度的功能。
函数定义
-1
水平扫描
-1
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]
因此,在第一行中,位置2至4和位置10至11有wherearethey[array, "Row"]
。
垂直扫描
Row {{From, To}}
1 {{2,4},{10,11}}
3 {{6,7}}
4 {{8,9}}
5 {{11,12}}
7 {{13,14}}
-1