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.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
2 commentaires
Réponse acceptée
John BG
le 2 Mar 2017
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
Plus de réponses (4)
Guillaume
le 24 Fév 2017
repelem([1;2;3], [14, 20, 50])
3 commentaires
Guillaume
le 27 Fév 2017
Modifié(e) : Guillaume
le 27 Fév 2017
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.
Jan
le 27 Fév 2017
Modifié(e) : Jan
le 27 Fév 2017
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.
John BG
le 2 Mar 2017
Modifié(e) : John BG
le 2 Mar 2017
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 commentaire
Jan
le 2 Mar 2017
Modifié(e) : Jan
le 3 Mar 2017
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
Voir également
Catégories
En savoir plus sur Bartlett dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!