Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

fixing the values in matrix when it come between 0 and 1

조회 수: 1 (최근 30일)
Offroad Jeep
Offroad Jeep 2015년 9월 10일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi all,I want to fix the values in matrix which comes between 0 and 1 and the other values carry on till the time when all the values are between 0 and 1. when all the values are between 0 and 1 the program should break.
a = randi(5,5)
nmc = 1000
for i = 1:nmc
b = rand(1)
c = a - b
if c < a
a = c
if (a > 0) && (a < 1)
break;
end
end
end
  댓글 수: 3
Offroad Jeep
Offroad Jeep 2015년 9월 10일
Thanks it was quite informative..............
Image Analyst
Image Analyst 2015년 9월 10일
Then put it into action. Your code is still not formatted properly.

답변 (2개)

Image Analyst
Image Analyst 2015년 9월 10일
편집: Walter Roberson 2015년 9월 10일
I think you're trying to do this:
% Generate random numbers between 1 and 5.
a = randi(5,5)
nmc = 10000
for i = 1 : nmc
b = rand(1);
c = a - b; % c is a 5x5 matrix.
lessThanA = c < a;
% If c is less than a (which it will be),
% replace a with c. a will be smaller.
a(lessThanA) = c(lessThanA);
if all(a(:)>0 & a(:)<1)
break;
end
end
a
but I never got it to meet the condition for breaking.
  댓글 수: 2
Offroad Jeep
Offroad Jeep 2015년 9월 10일
Not working..........
Image Analyst
Image Analyst 2015년 9월 11일
That's because I did exactly what you asked for, which is not what you really want. I think Hamoon's latest solution, where b is some fraction of a, is probably what you want. If it is, please officially "Accept" his solution.

Hamoon
Hamoon 2015년 9월 10일
편집: Hamoon 2015년 9월 10일
Thank you image Analyst, I just changed the code a little bit, I think this is what 4*4 wants:
a = randi(5,5);
while true
condition = a>=0 & a<=1;
b = rand(1);
c = a - b; % c is a 5x5 matrix.
lessThanA = c < a;
% If c is less than a and condition isn't met,
% replace a with c. a will be smaller.
indexes = lessThanA & ~condition;
a(indexes) = c(indexes);
if all(condition)
break;
end
end
a
  댓글 수: 5
Offroad Jeep
Offroad Jeep 2015년 9월 10일
Dear Hamoon thanks for your efforts. I will let you know what i want. I have random direction of mangetic moments. when magnetic field is applied at an angle theta, they all start getting aligned with the magnetic field. when random numbers are subtracted all numbers should come in between 0 and 1 so that all the values come in same range and hence i can proceed with my simulations further this is the first step of my simulations for exchange bias. .... In more easy words I am trying to simulate FERROMAGNETISM.................. Regards and many thanks for your concern and efforts......
Hamoon
Hamoon 2015년 9월 11일
It's OK, I got the problem here, the point is that "b" should NOT be a simple random value, you'd better calculate "b" based on magnetic field alignment formulation, but if you want to simply use random numbers for "b", you should consider that "b" is dependent to "a", in other words the amount that you subtract from "a" to force it to be aligned according to magnetic field, is dependent to "a". Here, i'm considering "a" shows the differences between orientation of a magnetic domain and an external magnetic field direction applied to that domain. So here, if a=0 then "b" should be zero.So "b" cannot be a simple random number. considering this fact, I just changed "b" a little bit and here is the code:
a = randi(5,5);
while true
b = rand(1)*a;
a = a - b;
if all(a>0 & a<1)
break;
end
end
a
here "b" is a random matrix, but it is dependent to "a", actually "b" is a random proportion of "a". I implemented the dependency of b to a using b=rand*a you may want to do something else, but it's all about "b", you don't need to change anything else.
here I also didn't define "c" variable, we don't need that.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by