array muddle problem
Afficher commentaires plus anciens
Hi,
I have an array composed of pairs of variables,
X=[a1 a2 b1 b2 c1 c2..]
and I want to modify each PAIR by a small random percentage. My percentage modifier is
percent=linspace(0.95,1.05,100);
rand_percent=randperm(length(percent));
modifier=percent(rand_percent(1:length(X)));
Since 'modifier' is the same length as 'X', if I multiply them together I modify every element of 'X'. However, I need to modify each PAIR by the same percentage, ie
X=[0.99a1 0.99a2 1.02b1 1.02b2 0.95c1 0.95c2..]
I'm getting lost in the arrays on this one, any suggestions?
Réponse acceptée
Plus de réponses (1)
Paulo Silva
le 7 Avr 2011
Laura was faster than me and her code is simple, here's my version
X=[1 2 3 4 5 6]; %just modify X, the code should handle more pairs
lhX=length(X)/2;
percent=linspace(0.95,1.05,lhX);
rand_percent=randperm(length(percent));
modifier=percent(rand_percent(1:lhX));
modifier1=repmat(modifier,2,1); %copy modifier to another line
XX=reshape(X,2,lhX); %put pairs in the same column
XXX=modifier1.*XX; %do the calculation
Xnew=reshape(XXX,1,numel(XXX)); %put all in one line
%Xnew %uncoment this line to see the result
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!