repeat elements of row matrix li [1 2 2 3 3 3 4 4 4 4 n...... n times]

7 visualizaciones (últimos 30 días)
Ankush Kumar
Ankush Kumar el 13 de Sept. de 2020
Comentada: Adam Danz el 15 de Sept. de 2020
creat a row matrix like
n=input
eg n=4
row=[1 2 2 3 3 3 4 4 4 4 ]
  1 comentario
Adam Danz
Adam Danz el 15 de Sept. de 2020
Response to your edited question:
Now the problem is very easy, see the last example in my answer.

Iniciar sesión para comentar.

Respuestas (2)

KSSV
KSSV el 13 de Sept. de 2020
n = 6 ;
p = cumsum(10.^(0:n-1)) ;
iwant = p.*(1:n)
  2 comentarios
KSSV
KSSV el 13 de Sept. de 2020
n = 10 ;
iwant = cells(1,n) ;
for i = 1:n
iwant{i} = (repmat(num2str(i),1,i)) ;
end
iwant

Iniciar sesión para comentar.


Adam Danz
Adam Danz el 13 de Sept. de 2020
Editada: Adam Danz el 15 de Sept. de 2020
If you want the output to be character vectors,
n = 12;
y = arrayfun(@(n){repmat(num2str(n),1,n)},1:n);
% Output Sample:
% y =
% 1×12 cell array
% Columns 1 through 10
% {'1'} {'22'} {'333'} {'4444'} {'55555'} {'666666'} {'7777777'} {'88888888'} ....
If you want the output to be numeric,
n = 12;
y = cell2mat(arrayfun(@(n){str2double(repmat(num2str(n),1,n))},1:n));
% Output Sample:
format longg
% y = 1 22 333 4444 55555 666666 7777777 88888888 999999999 1.01010101010101e+19 1.11111111111111e+21 1.21212121212121e+23
If you want the output to be numeric and match the format of your updated question,
n = 12;
y = repelem(1:n, 1:n);
% Output
% y =
% Columns 1 through 26
% 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7
% Columns 27 through 52
% 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10
% Columns 53 through 78
% 10 10 10 11 11 11 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by