i want to write shorter code

3 visualizaciones (últimos 30 días)
Luca Re
Luca Re el 1 de Sept. de 2023
Editada: Voss el 1 de Sept. de 2023
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

Respuesta aceptada

Voss
Voss el 1 de Sept. de 2023
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 comentarios
Bruno Luong
Bruno Luong el 1 de Sept. de 2023
b|b'
is even shorter. ;-)
Voss
Voss el 1 de Sept. de 2023
Editada: Voss el 1 de Sept. de 2023
@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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by