Creating a matrix within a matrix

조회 수: 1 (최근 30일)
Jakub
Jakub 2013년 2월 21일
I'm having trouble on how to create an MxN matrix will all zeros and in that MxN matrix I want the the first column, first row, last row, and last column to be zeros while the rest of the matrix is all different numbers, with possible values as 0 zeros as well. An example is below,
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 0 1 1 1 1 1 0 0 0
0 0 1 1 0 1 0 0 0 1 1 1 1 0
0 1 0 1 1 1 1 0 1 0 1 0 1 0
0 1 0 0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
Really the only values that the middle section can have is one or zero but the outsides have to all be zeros.
  댓글 수: 3
Jakub
Jakub 2013년 2월 21일
Well the first step I tried was doing zeros(M,1,1) and then (zeros(1,N,1) and got stuck with that because that only provided me with the first row and column. Next I tried doing a zeros(M,N) but that leaves me with a MxN zero matrix.
Jakub
Jakub 2013년 2월 21일
It's actually part of my Thesis however I have no prior computer programing background and my adviser took the week off ~_~

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2013년 2월 21일
편집: Matt J 2013년 2월 21일
If you're saying you have a given matrix A and want to set the edges to zero,
A([1,end],[1,end])=0;
  댓글 수: 2
Jakub
Jakub 2013년 2월 21일
Alright I understand that, thank you. One last question would be how do I randomly pick values either 0 or 1 inside the submatrix I want. Meaning I leave the outside all zeros but generate possible 1 or 0 on the inside.
Matt J
Matt J 2013년 2월 21일
Use randi() to generate random zeros and ones everywhere. Then use the code I showed you to set the boundaries to zero.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by