Flipping the numbers 1 to -1

Hi, to all the members. Consider a matrix 'a' now we take a random number, if random number is >= to 0.5 change the first element from 1 to -1 then calculate mag,if not keep it as 1...... then for next step if random number is < 0.5 keep the matrix and calculate mag, now again take random number and check if now the random number is >=0.5 flip the second element calculate mag.......etc keep on going till the 25th element.
for example
a = 1 1 1 1 random = 0.2324 first element remains same mag = 4
now again take random number random = 0.5649 now a becomes a = -1 1 1 1 mag = 2 now again take random random = 0.6978 a = -1 -1 1 1 mag = 0
random = 0.2345 a = -1 -1 1 1 mag = 0
random = 0.7995 a = -1 -1 -1 1 mag = -1
random = 0.3453 a = -1 -1 -1 1 mag = -1
random = 0.9874 a = -1 -1 -1 -1 mag = -4
end the loop
show mag values and plot against B mag = 4 2 0 0 -1 -1 -4
B =linspace(2,0,numel(mag)) plot(B,mag)
thanks for guidance

7 comentarios

Stephen23
Stephen23 el 11 de Feb. de 2016
Should those mag = -1 really be -2 ?
Jos (10584)
Jos (10584) el 11 de Feb. de 2016
What exactly is your question?
Offroad Jeep
Offroad Jeep el 11 de Feb. de 2016
I have tried to define the question in explanation.
Jos (10584)
Jos (10584) el 11 de Feb. de 2016
Do you have problems coding this? What did you try so far?
Offroad Jeep
Offroad Jeep el 11 de Feb. de 2016
I have attached the m file........
Offroad Jeep
Offroad Jeep el 11 de Feb. de 2016
I will be grateful if you can help me in this regard.......... and i think i will be able to solve the problem where i want to fit this logic.......

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 11 de Feb. de 2016
Editada: Stephen23 el 12 de Feb. de 2016

0 votos

Try using a while loop:
a = ones(1,4)
k=1;
mag=[];
while k<=numel(a)
x = rand()>=0.5;
a(k)=a(k)-2*x;
mag(end+1)=sum(a);
k=k+x;
end
Produces:
a =
-1 -1 -1 -1
mag =
4 4 2 2 2 0 -2 -4

7 comentarios

Offroad Jeep
Offroad Jeep el 11 de Feb. de 2016
Can i change it for 4 X 4 matrix........
Stephen23
Stephen23 el 11 de Feb. de 2016
Of course you can change it to work with a matrix. If you told me how you want it to work with a matrix then I could help you too! Do you want to work along each row, or column, or along all elements of the matrix (row-wise or column-wise) ?
Offroad Jeep
Offroad Jeep el 11 de Feb. de 2016
randomly pick the element and flip it from 1 to -1 till all the elements are gone from 1 to -1
Stephen23
Stephen23 el 12 de Feb. de 2016
Editada: Stephen23 el 12 de Feb. de 2016
Well, that is easy to do:
a = ones(4,4);
k=1;
mag=[];
while k<=numel(a)
x = rand()>=0.5;
a(k)=a(k)-2*x;
mag(end+1)=sum(a(:));
k=k+x;
end
Note the change from a to a(:) so that it works for a matrix too. Here is the output:
>> mag
mag =
16 16 16 14 12 10 10 10 10 8 6 6 4 2 2 2 2 0 0 0 -2 -4 -6 -8 -10 -10 -10 -12 -14 -14 -14 -14 -16
Offroad Jeep
Offroad Jeep el 12 de Feb. de 2016
Thanks. Thats great......... will you like to work together for a research paper........
Stephen23
Stephen23 el 12 de Feb. de 2016
If my answer resolves your question then please accept it.

Iniciar sesión para comentar.

Más respuestas (1)

Jos (10584)
Jos (10584) el 11 de Feb. de 2016

1 voto

help numel
help for
help rand
help if
help sum
% for k=1:N
% if ...
% A(k) = -A(k)
% B(k) = sum ...
% end

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Feb. de 2016

Comentada:

el 17 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by