Effacer les filtres
Effacer les filtres

Multiply nth elements of an array by an element of a different array?

4 vues (au cours des 30 derniers jours)
Ashton Linney
Ashton Linney le 19 Avr 2020
Commenté : dpb le 20 Avr 2020
I have the following code.
I would like it to work such that if the first element of Ox is greater than zero, which in this case it is, the 1st, 5th, 9th, 13th, 17th, 21st, 25th, etc up to the end of Array will be multiplied by the first element of Ox.
So the output would be:
AllOx1 = (0.5 1 1 1 0.5 1 1 1 0.5 1… etc)
Ox = [0.5 0 0 0];
Array = ones(1,256);
AllOx1 = [];
if Ox(1) > 0
AllOx1 = (Array(1:4:end)*Ru(1), 1, []);
end
Any help would be much appreciated!
Thank you
  2 commentaires
the cyclist
the cyclist le 19 Avr 2020
I have a couple questions.
Your question does not mention the variable Ru, but your code uses it. Why?
What do you want AllOx1 to be if Ox is not greater than zero?
Ashton Linney
Ashton Linney le 20 Avr 2020
Ru is another variable I have in my code, but accidently included it here. Ru was meant to be Ox, which will have made a lot more sense for you, thank you for pointing that out to me :)

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 19 Avr 2020
Modifié(e) : dpb le 19 Avr 2020
I'll presume the answer to Cyclist's 2nd Q? is to be unchanged...and the other is just using different variables for the posting...either way, the answer is essentially the one given removing some unnecessary fluff...but adding one critical piece:
A=ones(1,10); Ox=[0.5 0 0]; % preliminaries...
% engine
B=A; % must copy original else will be short unless mod(size(A),4)==0
if Ox(1)>0
B(1:4:end)=B(1:4:end)*Ox(1);
end
>> B % results...
B =
0.5000 1.0000 1.0000 1.0000 0.5000 1.0000 1.0000 1.0000 0.5000 1.0000
>>
  2 commentaires
Ashton Linney
Ashton Linney le 20 Avr 2020
That achieves exactly what I wanted it to. Thank you very much sir!
You were correct in your assumption :)
dpb
dpb le 20 Avr 2020
Occasionally the Crystal Ball Toolbox does come in handy... :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by