当我尝试执行标量乘法时,我的矩阵不会改变

时间:2017-01-20 06:02:57

标签: java arrays matrix

我们应该将矩阵中的每个元素乘以一个数字,在这种情况下" k"。不确定我做错了什么,但它不会工作。救命!到目前为止,我已经编辑并添加了整个项目。

public class Matrix {

    private int r;//rows
    private int c;//columns
    private int[][] neo; //2D array

    public static void main(String[] args) {
        Matrix m1 = new Matrix(3,4);
        Matrix m2 = new Matrix(3,4);

        System.out.println(m2);
        try {
            Matrix m3 = m1.multiply(m2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        m2.scaleMult(k);
    }//main

    public Matrix(int row, int column) {
        r = row;
        c = column;
        neo = new int[r][c];
        for(int i = 0; i < neo.length; i++) {
            for (int j = 0; j < neo[i].length; j++) {
                neo[i][j] = (int) (1 + Math.random() * 100);
            }//forLoop
        }//forLoop
        System.out.println(Arrays.toString(neo));
    }//Matrix
    public Matrix copyMatrix(Matrix m) {
        Matrix copy = new Matrix(m.r, m.c);
        for (int i = 0; i < r; i++) {
            System.arraycopy(this.neo[i], 0, copy.neo[i], 0, this.neo[i].length);
        }//forLoop
        return copy;
    }//copyMatrix
    public void scaleMult(int k){

        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)

        this.neo[i][j] * k;

    }//scaleMult
    public boolean equals(Matrix m2) {
        if (this.r != m2.r || this.c != m2.c) {
            return false;
        }
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (this.neo[i][j] != m2.neo[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }//equalsMethod
    public Matrix multiply(Matrix m2) throws Exception {
        if (this.c != m2.r) {
            throw new RuntimeException();
        }//if

        Matrix m3 = new Matrix(this.r, m2.c);
        for (int i = 0; i < this.r; i++) {
            for (int j = 0; j < m2.c; j++) {
                m3.neo[i][j] = 0;
                for (int k = 0; k < this.c; k++) {
                    m3.neo[i][j] += this.neo[i][k] * m2.neo[k][j];
                }//forK
            }//forJ
        }//forI
        return m3;
    }//multiplyMethod

}//class

2 个答案:

答案 0 :(得分:0)

如果你的问题乘以矩阵乘以数字,那么这是一个非常简单的例子。

 public static void main(String []args){
    int[][] a = {{1,2,3},{4,5,6}};
    int[][] b=new int[2][3];
    int i,j,k=2;
    for(i=0;i<2;i++)
        for(j=0;j<3;j++)
            b[i][j]=a[i][j]*k;
    for(i=0;i<2;i++)
        for(j=0;j<3;j++)
            System.out.println(b[i][j]);
 }

左边必须有一些数组变量,应该赋予乘法值。

这有帮助吗?如果没有,请发布完整的代码。

答案 1 :(得分:0)

将您的class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate { @IBOutlet weak var tableView: UITableView! var searchController: UISearchController! var userContent = [Sneakers]() var filteredSearchSneakerNames = [Sneakers]() var filteredSearchSneakerConditions = [Sneakers]() override func viewDidLoad() { super.viewDidLoad() self.searchController = UISearchController(searchResultsController: nil) //search controller initializers... self.tableView.reloadData() } deinit{ self.searchController = nil } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let scopeButtonIndex = self.searchController.searchBar.selectedScopeButtonIndex if scopeButtonIndex == 0{ return self.filteredSearchSneakerNames.count }else{ return self.filteredSearchSneakerConditions.count } } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("SearchCell", forIndexPath: indexPath) let scopeButtonTitles = self.searchController.searchBar.selectedScopeButtonIndex if scopeButtonTitles == 0 { let searchString = self.filteredSearchSneakerNames[indexPath.row] cell.textLabel?.text = searchString.name cell.detailTextLabel?.text = "" }else{ let searchString = self.filteredSearchSneakerConditions[indexPath.row] cell.textLabel?.text = searchString.name cell.detailTextLabel?.text = searchString.condition } return cell } func didPresentSearchController(searchController: UISearchController) { self.searchController.searchBar.becomeFirstResponder() } func searchBarTextDidBeginEditing(searchBar: UISearchBar) { self.tableView.reloadData() } func updateSearchResultsForSearchController(searchController: UISearchController) { self.filteredSearchSneakerNames.removeAll(keepCapacity: false) self.filteredSearchSneakerConditions.removeAll(keepCapacity: false) let searchText = searchController.searchBar.text let scopeButtonIndex = self.searchController.searchBar.selectedScopeButtonIndex if scopeButtonIndex == 0{ let searchPredicate = NSPredicate(format: "SELF.name CONTAINS[c]%@", searchText!) let array = (self.userContent as NSArray).filteredArrayUsingPredicate(searchPredicate) self.filteredSearchSneakerNames = array as! [Sneakers] }else{ let searchPredicate = NSPredicate(format: "SELF.condition CONTAINS[c]%@", searchText!) let array = (self.userContent as NSArray).filteredArrayUsingPredicate(searchPredicate) self.filteredSearchSneakerConditions = array as! [Sneakers] } self.tableView.reloadData() } 方法更改为此方法,它应该有效:

scaleMult

此外,根据您在public void scaleMult(int k) { for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { this.neo[i][j] *= k; // you forgot to assign the value, basically we multiply neo[i][j] by k and make neo[i][j] receive that value // making this.neo[i][j] = this.neo[i][j] *k; would work as well } } }//scaleMult 方法中提供的示例,您的代码将在main方法上启动异常,因为如您指定的那样,如果一个列的数量,您只能乘以两个矩阵等于对方的行数。除此之外,我建议为主要方法创建一个新类。