我有一个矩阵A=(1 2; 3 4)
,我想将A
增加到B = (1 2 0; 3 4 0; 0 0 0)
,我该怎么办?
一种方法是:B = [[A, zeros(2,1)]; zeros(1,3)]
但是在动态过程中,任何其他想法都可能是笨拙的?
答案 0 :(得分:5)
B=A
B(3,3)=0
Matlab使用零自动填充其他元素
答案 1 :(得分:1)
此外:
// Allow short name access to java.awt.Color
import java.awt.Color;
/*
* This is the shell for assignment: MyScene.
* This program draws a simple scene using additional
* classes that the student creates.
*
* @author Dan Jinguji
* @author Chris Wilson
* @version Assignment 4: MyScene
*/
public class MyScene extends NscWindow {
// instance variables
private MyHouse house1;
private MyHouse house2;
private MyHouse house3;
private MyTree tree1;
private MyTree tree2;
private MyTree tree3;
/**
* Constructor for objects of class MyScene
*/
public MyScene() {
// Specify the constructor for the superclass
super(10, 10, 400, 300);
// set characteristics for the object
setTitle("My Scene");
// Draw the sky
getContentPane().setBackground(new Color(0x00, 0xCC, 0xFF));
// Draw some grass
NscRectangle grass = new NscRectangle(0, 100, 400, 200);
grass.setBackground(new Color(0x00, 0x99, 0x00));
grass.setFilled(true);
add(grass);
// instantiate new house objects
house1 = new MyHouse(100, 50, Color.white);
add(house1);
house2 = new MyHouse(200, 150);
add(house2);
house3 = new MyHouse(0, 200, Color.orange);
add(house3);
// request a rerendering of the window
repaint();
// instantiate new tree objects
tree1 = new MyTree(35, 20, Color.white);
add(tree1);
tree2 = new MyTree(290, 50);
add(tree2);
tree3 = new MyTree(5, 60, Color.cyan);
add(tree3);
// request a rerender
repaint();
}
/**
* Alter the scene, by changing color
*/
public void change1() {
// change the color of the houses and trees
house1.setColor(Color.red);
tree1.setColor(Color.black);
house2.setColor(Color.magenta);
tree2.setColor(Color.orange);
house3.setColor(Color.black);
tree3.setColor(Color.pink);
// request a rerendering of the window
repaint();
}
/**
* Alter the scene, by changing location
*/
public void change2() {
// change the location of the houses and trees
house1.setLocation(50, 125);
house2.setLocation(150, 25);
house3.setLocation(0, 150);
tree1.setLocation(25, 10);
tree2.setLocation(300, 20);
tree3.setLocation(150, 90);
// request a rerendering of the window
repaint();
}
/**
* Alter the scene, by changing size of objects and underlying containers
*
*/
public void change3() {
house1.setSize(80, 60);
}
/**
* Alter the scene, restoring original settings
*/
public void reset() {
// reset the initial values for the house
house1.setLocation(100, 50);
house1.setColor(Color.white);
house1.setSize(120, 90);
// reset the initial values for the house
house2.setLocation(200, 150);
house2.setColor(Color.blue);
house2.setSize(120, 90);
// reset the initial values for the house
house3.setLocation(0, 200);
house3.setColor(Color.orange);
house3.setSize(120, 90);
// rest the initial values for the tree
tree1.setLocation(35, 20);
tree1.setColor(Color.white);
tree1.setSize(60, 120);
// rest the initial values for the tree
tree2.setLocation(290, 50);
tree2.setColor(Color.green);
tree2.setSize(60, 120);
// rest the initial values for the tree
tree3.setLocation(5, 90);
tree3.setColor(Color.cyan);
tree3.setSize(60, 120);
// request a rerendering of the window
repaint();
}
/**
* The application method, to test your code
*
* @param args The command-line arguments
*/
public static void main(String[] args) {
// declare a MyScene reference
MyScene aScene;
// instantiate MyScene
aScene = new MyScene();
// pause
javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");
// test setColor
aScene.change1();
javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");
// test setLocation
aScene.change2();
javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");
// test setSize
aScene.change3();
javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");
// reset initial values
aScene.reset();
}
}
输出:
padarray(A,[1 1],'post')
它更加通用,使语义非常清晰。