i want to write shorter code

1 次查看(过去 30 天)
b=[0 1 1 0 0]
b = 1×5
0 1 1 0 0
n=numel(b);
c=b'
c = 5×1
0 1 1 0 0
repmat(b,n,1)
ans = 5×5
0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0
repmat(c,1,n)
ans = 5×5
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
repmat(b,n,1)|repmat(c,1,n)
ans = 5×5 logical array
0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 0 0

采纳的回答

Voss
Voss 2023-9-1
b=[0 1 1 0 0]
b = 1×5
0 1 1 0 0
b|b.'
ans = 5×5 logical array
0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 0 0
  2 个评论
Voss
Voss 2023-9-1
编辑:Voss 2023-9-1
@Bruno Luong: Very good! And it works the same, since | won't accept non-real operands.
@Luca Re: In case you are interested in the difference:
% when b is real,
b = [0 1 1 0 0];
% b' (complex conjugate transpose) and b.' (transpose) are the same
isequal(b',b.')
ans = logical
1
% when b is non-real,
b = [0 1i 1 0 0];
% b' and b.' are different
isequal(b',b.')
ans = logical
0
% but it doesn't matter in this case because | (or) can't deal with
% non-real operands
try
b|b'
catch e
disp(['error: ' e.message])
end
error: Operands must be real.
try
b|b.'
catch e
disp(['error: ' e.message])
end
error: Operands must be real.
% so for b|b.' (or b|b') to work, b must be real, in which case
% b.' is the same as b' as shown above

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Electromechanical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by