Create Matrix with zeros, ones and some numbers.

I am struggling to create a following matrix in one go (using as few numbers used as possible). Can someone please help me out?
N =
0 0 0 0 0 1
0 0 0 0 0 1
1 2 3 4 5 1
0 2 4 6 8 1
8 7 2 5 9 1

6 comentarios

In the absence of any information as to how the numbers are determined
N = [0 0 0 0 0 1; 0 0 0 0 0 1; 1 2 3 4 5 1; 0 2 4 6 8 1; 8 7 2 5 9 1];
is the simplest way to create that matrix in one go!
Jos (10584)
Jos (10584) el 22 de Mzo. de 2019
Slightly shorter ;-)
N = [0 0 0 0 0 1; 0 0 0 0 0 1; 1:5 1; 0:2:8 1; 8 7 2 5 9 1]
N = [zeros(1,5) 1; zeros(1,5) 1; 1:5 1; 0:2:8 1; 8 7 2 5 9 1]
maybe!
Personally, I like this, since it uses fewer numbers, sort of.
dec2base([1 1 123451 24681 872591],10) - '0'
But it depends on whether a multi-digit number is one number, or perhaps 6 numbers. It is fewer characters.
HARSH ZALAVADIYA
HARSH ZALAVADIYA el 25 de Mzo. de 2019
N = [[zeros(2,5); (1:1:5); (0:2:8); 8 7 2 5 9;],[ones(5,1)]]
The shortest I can Make.
@HARSH: A simplification:
N = [[zeros(2,5); 1:5; 0:2:8; 8 7 2 5 9], ones(5,1)]

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 25 de Mzo. de 2019
Editada: Jan el 25 de Mzo. de 2019
With 2 "numbers" only:
c = ['0 0 0 0 0 1 ', ...
'0 0 0 0 0 1 ', ...
'1 2 3 4 5 1 ', ...
'0 2 4 6 8 1 ', ...
'8 7 2 5 9 1']
sscanf(c, '%g', [6,5]).'
But it depends on the rules if characters are considered as "numbers". But what does "in one go" mean? Is calling functions like dec2base accepted also? Without dec2base:
floor(rem([1; 1; 123451; 24681; 872591] ./ 10.^(5:-1:0), 10))
Or is this "better":
floor(rem([1; 1; 123451; 24681; 872591] ./ flip(10.^(0:5), 10))

Más respuestas (1)

John D'Errico
John D'Errico el 25 de Mzo. de 2019
Editada: John D'Errico el 25 de Mzo. de 2019
Far fewer numbers required...
dec2base(hex2dec({'1','1','1E23B','6069','D508F'}),10) - '0'
ans =
0 0 0 0 0 1
0 0 0 0 0 1
1 2 3 4 5 1
0 2 4 6 8 1
8 7 2 5 9 1
Or, this, with even fewer numerics?
dec2base(base2dec({'1','1','2N97','J1L','IPAN'},36),10) - '0'
But more characters than my first solution.
dec2base([1 1 123451 24681 872591],10) - '0'

2 comentarios

Jan
Jan el 26 de Mzo. de 2019
+1. {'1', '1', '2N97', 'J1L', 'IPAN'} - this reminds me to programming in ZX81 Basic. I had 1kB of RAM and VAL '1' took less memory that a numerical 1.
John D'Errico
John D'Errico el 26 de Mzo. de 2019
True. Though I'm not sure my nostalgia is well placed for those great, wonderful days of decks of punched cards and great spools of paper tape. There was a trick with decks of cards - draw a diagonal line across the top of the card deck, so that if one accidentally dropped the box of cards on the floor, they could be more easily re-odered.
Those were days when everything we did with computers was completely different. Output was onto great reams of computer printout. One day, I made a mistake plotting a simple figure with what was probably a Calcomp plotter. So the next day I received a long roll of Calcomp paper. Good quality paper too, 3 feet wide, and perhaps 100 feet long before they stopped it, with only a long diagonal line drawn down the length. I brought it home to use as a painting dropcloth, useful for many years afterwards.
I don't miss the days that much when my main computer interface was toggling the dip switches on the front panel of a computer. But there was a lot of fun to be had back then too.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 22 de Mzo. de 2019

Comentada:

el 26 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by