I need to repeat numbers in an array with a certain number of repetitions for each value without(repelem or repmat)

조회 수: 4 (최근 30일)
list_1=[2;3;5;6] is array 1 or [1,4,5,6]
list_2=[1;4;3;1] Number of repetitions for each value in list_1 or [1,4,3,1]
I need the following output
[2;3;3;3;3;5;5;5;6]
or [2,3,3,3,3,5,5,5,6]
for example only one repetition from value 2 becaus of this first value in list_2 it says only one number from the first value in list_1
4 repetitions from number 3
3 repetitions from number 5
1 repetitions from number 6
i need the solution for huge list

채택된 답변

Akira Agata
Akira Agata 2022년 8월 1일
I'm not sure why you do not prefer repelem/repmat...
Anyway, how about the following solution?
% Example
list_1 = [2;3;5;6];
list_2 = [1;4;3;1];
% One possible solution without using repelem/repmat
C = arrayfun(@(x,y) x*ones(y, 1), list_1, list_2, 'UniformOutput', false);
list_3 = cell2mat(C);
% Show the result
disp(list_3)
2 3 3 3 3 5 5 5 6
  댓글 수: 2
Ezzaddin Al-Soufi
Ezzaddin Al-Soufi 2022년 8월 2일
Thanks a lot
for me it doesn't matter if i use repelem/repmat, if i used repelem or repmat i have the problem that i can't have the solution as array or vector. I tried this
n=1:1:4;
list_1 = [2;3;5;6];
list_2 = [1;4;3;1];
for i=1:1:length(n)
b=repelem(list_1(i),list_2(i),1)
end
b = 2
b = 4×1
3 3 3 3
b = 3×1
5 5 5
b = 6
Stephen23
Stephen23 2022년 8월 2일
편집: Stephen23 2022년 8월 2일
"for me it doesn't matter if i use repelem/repmat"
Your question title states "without(repelem or repmat)". If it does not matter, why tell us not to use them?
"if i used repelem or repmat i have the problem that i can't have the solution as array or vector."
REPELEM gives exactly the same output as Akira Agata's answer:
list_1 = [2;3;5;6];
list_2 = [1;4;3;1];
list_3 = repelem(list_1,list_2)
list_3 = 9×1
2 3 3 3 3 5 5 5 6

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

추가 답변 (2개)

Bruno Luong
Bruno Luong 2022년 8월 2일
편집: Bruno Luong 2022년 8월 2일
Why prefer a simple method when one can do in a complicated manner:
list_1 = [2;3;5;6]
list_1 = 4×1
2 3 5 6
list_2 = [1;4;3;1]
list_2 = 4×1
1 4 3 1
idx=cumsum(accumarray(cumsum([1; list_2(:)]),1));
list_1(idx(1:end-1))
ans = 9×1
2 3 3 3 3 5 5 5 6

Bruno Luong
Bruno Luong 2022년 8월 2일
편집: Bruno Luong 2022년 8월 2일
The old for-loop
list_1 = [2;3;5;6];
list_2 = [1;4;3;1];
r = zeros(sum(list_2),1);
start = 0;
for k = 1:length(list_2)
n = list_2(k);
r(start+1:start+n)) = list_1(k);
start = start + n;
end
r
r = 9×1
2 3 3 3 3 5 5 5 6
  댓글 수: 2
Bruno Luong
Bruno Luong 2022년 8월 2일
Some timing, for-loop seems to be the fatest
list_1 = randi(1000,1000,1);
list_2 = randi(2000,1000,1);
tic
r = zeros(sum(list_2),1);
start = 0;
for k = 1:length(list_2)
n = list_2(k);
r(start+1:start+n) = list_1(k);
start = start + n;
end
toc
Elapsed time is 0.008149 seconds.
tic
C = arrayfun(@(x,y) x*ones(y, 1), list_1, list_2, 'UniformOutput', false);
list_3 = cell2mat(C);
toc
Elapsed time is 0.016814 seconds.
tic
idx=cumsum(accumarray(cumsum([1; list_2(:)]),1));
r=list_1(idx(1:end-1));
toc
Elapsed time is 0.014714 seconds.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by