Creating an array of one value

I use matlab and want to check if the column vector is equal to another with 3dp, for this I am trying to create an array full of 0.001, and check whether it is greater or equal. is there an easier way than a for loop to create this array or not?

+3
source share
4 answers

Is there an easier way than a for loop to create this array or not?

Yes use

ones(size, 1) * myValue

for instance

>> ones(5,1)*123

ans =

   123
   123
   123
   123
   123
+10
source

So let me know if this is correct.

You have 2 vectors, aand beach with elements N. You want to test for each i<=N, abs(a(i)-b(i)) <= 0.001.

If this is correct, you want to:

vector_match = all(abs(a-b) <= 0.001);

vector_match is boolean.

+4

:

a = rand(1000,1);
b = rand(1000,1);

idx = ( abs(a-b) < 0.001 );
[a(idx) b(idx)]

» ans =
       0.2377      0.23804
       0.0563     0.056611
      0.01122     0.011637
        0.676       0.6765
      0.61372      0.61274
      0.87062      0.87125
+1

"", :

a = [ 0.005, -0.003 ];
x = find(a > 0.001);

FWIW, I found that comparing numbers in MATLAB is an absolute nightmare, but I'm also new to this. The fact is that when comparing, you may encounter problems with floating point comparisons, so keep this in mind when trying something (and someone, please correct me if I am wrong about this or have a great solution).

0
source

Source: https://habr.com/ru/post/1761850/


All Articles