Creating a mxn matrix that is all zeros except for the middle row and middle column which are 1s

70 ビュー (過去 30 日間)
Hi,
Given an 7x5 matrix, i am to return a mxn matrix M that is all zeros except for the middle row and middle column, which should be all 1s.
I have gotten as far as creating a zeros matrix using the code M=zeros(7,5), but am clueless on how to change the middle row and middle column to 1s.
I should also mention that the code should be a one-liner.
Thank you

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 28 日
編集済み: Wan Ji 2021 年 8 月 28 日
M = [0,0,0,1,0,0,0; 0,0,0,1,0,0,0; 1,1,1,1,1,1,1; 0,0,0,1,0,0,0; 0,0,0,1,0,0,0];
Once for all
Or you can do
a = zeros(5,7);
a((1:numel(a))>size(a,1)*(size(a,2)-1)/2&1:numel(a)<=size(a,1)*(size(a,2)+1)/2|mod(1:numel(a),size(a,1))==(size(a,1)+1)/2) = 1
The result is
a =
0 0 0 1 0 0 0
0 0 0 1 0 0 0
1 1 1 1 1 1 1
0 0 0 1 0 0 0
0 0 0 1 0 0 0
We can extend it to any matrix with both odd columns and rows.
a = zeros(9,11);
a((1:numel(a))>size(a,1)*(size(a,2)-1)/2&1:numel(a)<=size(a,1)*(size(a,2)+1)/2|mod(1:numel(a),size(a,1))==(size(a,1)+1)/2) = 1
Then a becomes
a =
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0

その他の回答 (3 件)

Awais Saeed
Awais Saeed 2021 年 8 月 28 日
M = zeros(7,5)
M(:,ceil(size(M,2)/2)) = 1
M(ceil(size(M,1)/2),:) = 1
  4 件のコメント
Lavorizia Vaughn
Lavorizia Vaughn 2021 年 8 月 28 日
編集済み: Lavorizia Vaughn 2021 年 8 月 28 日
Isn’t it possible by just using column vectors or row vectors?
Awais Saeed
Awais Saeed 2021 年 8 月 28 日
This is the shortest code that I can come up with. Why do you need a one line answer? You are not doing it through loops, this is vectorisation which is far better and faster than loops. It is more readable as well.

サインインしてコメントする。


Zoya
Zoya 2023 年 3 月 13 日
編集済み: DGM 2023 年 3 月 14 日
% create a 6x6 matrix of zeros
matrix = zeros(6);
% set the middle two rows and columns to 1's
matrix(3:4, :) = 1;
matrix(:, 3:4) = 1;

John D'Errico
John D'Errico 2023 年 3 月 29 日
編集済み: John D'Errico 2023 年 3 月 31 日
Clearly a homework assignment. But now long since past, and with multiple answers, most of which are not in one line. However, simple enough to do, in one line in at least one way. Here is my first try:
A1 = (1:7)' == 4 | (1:5) == 3
A1 = 7×5 logical array
0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
Short. Sweet. Simple enough. I'm not sure I can beat that. It is logical, so if you needed it to be composed of doubles, that too is not difficult. Just use a unary plus. It forces me to add a parens though.
A2 = +((1:7)' == 4 | (1:5) == 3)
A2 = 7×5
0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
This next one uses a slight tweak on the diagonal, else there would be a 2 at the center element.
A3 = ((1:7)' == 4) + ((1:5) == 3) - ((1:7)' == 4) * ((1:5) == 3)
A3 = 7×5
0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
I'd bet I could do it in other ways too. How about this cute one?
A4 = dec2bin([4 4 4 31 4 4 4]) - '0'
A4 = 7×5
0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
This next one seems kind of silly, as you would never want to use it for a larger problem. But it works.
A5 = blkdiag(1,1,1,flip(eye(4)))*[zeros(6,4),ones(6,1);ones(1,5)]*blkdiag(1,1,flip(eye(3)))
A5 = 7×5
0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0
I'm sure there are other ways too. Personally, I prefer A1 and A4. The point is, as you learn to use MATLAB, you can learn different ways to manipulate the elements of arrays. But as you look for other ways to solve the problem, it forces you to learn about other functions you may never have seen in that context.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by