필터 지우기
필터 지우기

Fill in sequential numbers between two numbers

조회 수: 8 (최근 30일)
Tyler Smith
Tyler Smith 2016년 7월 7일
댓글: the cyclist 2016년 7월 7일
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.

채택된 답변

the cyclist
the cyclist 2016년 7월 7일
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
Tyler Smith
Tyler Smith 2016년 7월 7일
Thanks! Works great.
the cyclist
the cyclist 2016년 7월 7일
You could also substitute this line in place of the for loop:
C = cellfun(@(x,y)x:y,num2cell(A),num2cell(B),'UniformOutput',false)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by