如何合并矩阵中的元素

时间:2016-11-17 16:40:59

标签: matlab matrix

{
  "manifest_version": 2,

  "name": "QueueTube for YouTube!", 
  "short_name": "QueueTube", 
  "description": "Search YouTube without stopping the video, and make your own playlists!",
  "version": "1.5",
  "author": "Dara Javaherian",

  "permissions": ["tabs", "*://*.youtube.com/*"],

  "background": {
    "persistent":true,
    "scripts": [
      "bg/socket.io.js",
      "bg/background.js"
    ]
  },
  "icons": {
    "128": "icons/youtube-128.png"
  },
  "browser_action": {
    "default_icon": "icons/icon.png",
    "default_popup": "popup/popup.html"
  },
  "web_accessible_resources": [
    "spinner.gif"
  ],
  "content_scripts" : [{
    "matches" : 
      ["https://www.youtube.com/*",
      "http://www.youtube.com/*"],
    "js" : [
      "js/jquery.js",
      "js/inject.js"],
    "css" : ["styles/styles.css"]
  }]
}

我想摆脱所有零并合并两行

x = [1 2 3 4 5
     1 2 3 0 0];

2 个答案:

答案 0 :(得分:3)

nonzeros会在列向量中给出非零值,您只需要正确定位原始矩阵并转置为行向量(如果这是你想要的):

>> newx = nonzeros(x.').'
newx =

   1   2   3   4   5   1   2   3

答案 1 :(得分:1)

newx=x.';        %Taking Transpose
% Converting the given matrix into a column vector and then taking transpose again
% (since you require answer as a row vector)
newx=newx(:).' ; 
newx(newx==0)=[]  %Removing zeros

或使用reshape

newx = reshape(x.',1,[])
newx(newx==0)=[]  %Removing zeros

<强>结果:

newx =

     1     2     3     4     5     1     2     3