如何查找满足条件的矩阵元素 - 并将它们存储在另一个数组中

时间:2017-02-03 16:32:16

标签: arrays matlab

我有一个矩阵(名称为'im'),如下所示:

enter image description here

矩阵仅包含NaNs和字符‘A+’, ‘A-‘, ‘B+’ and ‘B-‘,但可以包含尽可能多的行。 我需要找到(行方式)任何jPositions我至少有‘A+’ and ‘B+’‘A+‘ and ‘B-‘‘A-‘, and ‘B-‘中的任何一个'A-' and 'B+'matrix P。我还想将这些职位存储在另一个Matrix P中,如下所示。

enter image description here

no. of rows = kmatrix P但k从一开始就不知道。请注意,jPositions中显示的2,3和4表示满足感兴趣条件的'im' For I = 1: size(im, 2) If any of q, r, s, t, u has at least one of ‘A+’ or ‘A-‘, and any of q, r, s, t, u has at least one of ‘B+’ or ‘B-‘, Then: -select that position. -Store the result in matrix P end end

我在下面有这个伪代码(不确定它是否正确),但我正在努力弄清楚如何编写代码。

<?php if($_POST){ require_once('connect.php'); try { $stmt = $con->prepare("INSERT INTO admin SET username=:username, fName=:fName, lName=:lName, email=:email, userLevel=:userLevel"); $username = htmlspecialchars(strip_tags($_POST['username'])); $fName = htmlspecialchars(strip_tags($_POST['fName'])); $lName = htmlspecialchars(strip_tags($_POST['lName'])); $email = htmlspecialchars(strip_tags($_POST['email'])); $userLevel = htmlspecialchars(strip_tags($_POST['userLevel'])); $stmt->bindParam(':username', $username); $stmt->bindParam(':fName', $fName); $stmt->bindParam(':lName', $lName); $stmt->bindParam(':email', $email); $stmt->bindParam(':userLevel', $userLevel); if($stmt->execute()) { echo "<h1>Success</h1>"; } else { echo "<h1>Fail</h1>"; } } catch(PDOException $exception) { die('Error: ' . $exception->getMessage()); } } ?> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"method="post"> <table id="createUser"> <tr> <td> Username </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> First Name </td> <td> <input type="text" name="fName"> </td> </tr> <tr> <td> Last Name </td> <td> <input type="text" name="lName"> </td> </tr> <tr> <td> email </td> <td> <input type="email" name="email"> </td> </tr> <tr> <td> Access Level </td> <td> <select name="userLevel"> <option selected disabled hidden>Please Select</option> <option value="0">Full Control</option> <option value="1">Managed Control</option> <option value="2">Own Page Control</option> </select> </td> </tr> <td> <input type="submit" value="Add User"/> <a href="admin.php">Test Link</a> </td> </table> </form> </body> </html>

对此有任何帮助,建议或建议非常感谢?提前谢谢。

1 个答案:

答案 0 :(得分:0)

可能有一个更优雅的解决方案,但这应该有效:

有你的矩阵:

im = {NaN NaN 'B-' NaN NaN; 'A-' NaN NaN 'B+' NaN; NaN 'A+' 'B+' NaN 'B-'; NaN NaN NaN 'B-' 'A+'};

您可以使用以下方式找到您的职位:

jPositions = find( ( any(strcmp('A+', im),2) | any(strcmp('A-', im),2) ) & ( any(strcmp('B+', im),2) | any(strcmp('B-', im),2)) );

strcmp返回逻辑矩阵。任何以2作为第二个参数返回一个逻辑向量,它指示每行天气它包含字符串。然后使用逻辑运算符组合它们,find返回行的索引为真。

返回:

jPositions = 2  3  4
相关问题