Fill in sequential numbers between two numbers

3 visualizaciones (últimos 30 días)
Tyler Smith
Tyler Smith el 7 de Jul. de 2016
Comentada: the cyclist el 7 de Jul. de 2016
I have two matrices, A = (1,2,3,4,5) and B = (6,8,14,12,11). I need to generate an array in which all the numbers between A(row,1) and B(row,1), A(row,2) and B(row,2), etc. are filled in. It should look like this: outputarray = (1,2,3,4,5,6,2,3,4,5,6,7,8,3,4,5,6,7,8,9,10,11,12,13,14...). So the numbers between 1 and 6 (the first cells of A and B) would get filled in and so on down the line.

Respuesta aceptada

the cyclist
the cyclist el 7 de Jul. de 2016
Here's one way:
A = [1,2,3,4,5];
B = [6,8,14,12,11];
N = numel(A);
C = cell(1,N);
for ni = 1:N
C{ni} = A(ni):B(ni);
end
output = [C{:}]
  2 comentarios
Tyler Smith
Tyler Smith el 7 de Jul. de 2016
Thanks! Works great.
the cyclist
the cyclist el 7 de Jul. de 2016
You could also substitute this line in place of the for loop:
C = cellfun(@(x,y)x:y,num2cell(A),num2cell(B),'UniformOutput',false)

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 7 de Jul. de 2016
C = cellfun(@colon,mat2cell(A,1,ones(1,numel(A))),mat2cell(B,1,ones(1,numel(B))),'uni',false);
result = [C{:}];

Categorías

Más información sobre Programming 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