필터 지우기
필터 지우기

how to make a colum of multiple 1 to 5. i want to make column vector of [1;1;1;1;1​;1;1;1;1;1​;1;1;1;1;2​;2;2;2;2;2​;2;2;2;2;2​;2;2;2;3;3​;3;3;3;3;3​;3;3;3;3;3​;3]. is there any way to make this kind of vector instead of typing this many times.

조회 수: 2 (최근 30일)
i want to print [1;1;1;1;1;1;1;1;1;1;1;1;1;;1;1;1;1;1;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;3;3;3;3;;33;3]. but without typing numbers all the time. how to print 14 times 1, 20 times 2, 50 times 3 in one column vector.

채택된 답변

John BG
John BG 2017년 3월 2일
even easier
A=[ones(1,1) 2*ones(1,20) 3*ones(1,50)]'
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  댓글 수: 3
John BG
John BG 2017년 3월 4일
편집: John BG 2017년 3월 4일
Pooja
thanks very much for accepting my answer, sometimes the simplest solution is the best solution
:)
regards
John BG

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

추가 답변 (4개)

Guillaume
Guillaume 2017년 2월 24일
repelem([1;2;3], [14, 20, 50])
  댓글 수: 3
Guillaume
Guillaume 2017년 2월 27일
편집: Guillaume 2017년 2월 27일
You can define repelem with:
repelem = @(rowv, reps) cell2mat(arrayfun(@(rowv, reps) repmat(colv, reps, 1), colv, reps, 'UniformOutput', false));
And call it with
repelem([1;2;3], [14;20;50]) %BOTH inputs MUST be column vectors
Note that the above is not a generic replacement for repelem, but one that will work for your case.
Pooja Patel
Pooja Patel 2017년 3월 3일
This is done with using repmat function. [repmat([1],14,1); repmat([2],20,1)] Thank you

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


Jan
Jan 2017년 2월 27일
편집: Jan 2017년 2월 27일
RunLength([1;2;3], [14, 20, 50])
Or:
B = [1, 2, 3];
N = [14, 20, 50];
Accum = cumsum([1, N]);
Index = zeros(1, sum(N));
Index(Accum(1:end - 1)) = 1;
Index = cumsum(Index);
Result = B(Index);
The idea is to create an index vector, which is as long as the output and contains a 1 on every position of a new value. Then after cumsum-ming this vector contains the index related to the input values repeated N times.
If you are in doubt about such smart indexing tricks, create a simple loop:
C = cell(1, numel(B));
for iC = 1:numel(B)
C{iC} = repmat(B(ic), N(iC), 1);
end
Result = cat(1, C{:});
Now the parts are created one after the other and stored in a cell. Finally all parts are concatenated. This is not very efficient, but perhaps you do not want to do this millions of times.
  댓글 수: 2
Pooja Patel
Pooja Patel 2017년 3월 3일
Error using RunLength (line 70) Compilation failed.
but second one the cumcum one and repmat one are working very well. thank you so much for your support

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


John BG
John BG 2017년 3월 2일
편집: John BG 2017년 3월 2일
thanks Guillame for pointing that out
Hi Pooja Patel
even easier
1. for the data given in the question
A=[ones(1,1) 2*ones(1,20) 3*ones(1,50)]'
or for any data, modify or replace the randi lines accordingly
V=[]
vc=randi([-10 10],5,1) % repeat this
repc=randi([2 10],5,1) % n times each
for k=1:1:length(vc)
V=[V ;vc(k)*ones(repc(k),1)];
end
V(1)=[]
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  댓글 수: 1
Jan
Jan 2017년 3월 2일
편집: Jan 2017년 3월 3일
V(1)=[] is a typo. There is no reason to delete the first element of the output.
Letting an array grow iteratively is extremely inefficient. The costs grow exponentially and therefore the editor shows the MLint warning in the line "V = [V; ...]":
The variable 'V' appears to change size on every loop iteration.
Consider preallocating for speed.
Pre-allocation is the first rule to be considered for writing efficient Matlab code.
Try it:
function myTest
B = (1:4000).';
N = repmat(20, size(B));
tic
for k = 1:10
V = test1(B, N);
end
toc
tic
for k = 1:10
V = test2(B, N);
end
toc
function V = test1(B, N)
V = [];
for k=1:1:length(N)
V=[V ;B(k)*ones(N(k),1)];
end
function V = test2(B, N)
Accum = cumsum([1; N(:)]);
Index = zeros(1, sum(N));
Index(Accum(1:end - 1)) = 1;
Index = cumsum(Index);
V = B(Index);
>> Elapsed time is 2.164152 seconds. % Iterative growing
>> Elapsed time is 0.009183 seconds. % Logical indexing
And exponential growing means, that for an input with the double size, the code needs much more than the double size. For 8000 elements:
Elapsed time is 11.990776 seconds. % Iterative growing
Elapsed time is 0.026229 seconds. % Logical indexing

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


Pooja Patel
Pooja Patel 2017년 3월 3일
this is done by using repmat function. [repmat([1],14,1); repmat([2],20,1)] Thank you

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by