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

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 日

0 投票

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 日

0 投票

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 件のコメント

Hamoon
Hamoon 2015 年 9 月 10 日
@4*4: I really don't know why you are doing this, you could say a=rand(5,5) from the beginning and that was what you wanted. But for this code you'll get a better result if you define:
a = randi([2,5],[5,5]);
and instead of d=rand(1) you can use:
b=rand(5,5);
Image Analyst
Image Analyst 2015 年 9 月 10 日
Well he said "when all the values are between 0 and 1 the program should break" and your program does the opposite but maybe that's what he wants. And the logic is still flawed because c will ALWAYS be less than a for every element, so there's no sense even checking for that.
Hamoon
Hamoon 2015 年 9 月 10 日
Yes you're right, there is no point in defining lessThanA variable, but the way he said "Not working..........", I thought he wants something like this. But the condition situation is defined like before.
so it's something like this:
a = randi([2,5],[5,5]);
while true
condition = (a>=0 & a<=1);
b = rand(5,5);
c = a - b;
indices = ~condition;
a(indices) = c(indices);
if all(condition)
break;
end
end
a
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.

この質問は閉じられています。

質問済み:

2015 年 9 月 10 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by