Comparing two matrices in Matlab - matlab

I have two matrices x and y, both are results from different algorithms/routines that are supposed to calculate the same result. While I know that the isequal() would check if x and y are the same matrix, the entries in those matrices would not be exactly the same (i.e. some entries may be with 5% off in worst case scenario). In this scenario, what would be the best method of comparing them to see if they are close enough to be considered the same result? Thanks in advance for the advices.

Try this:
tf = abs((A-B)./B)<0.05
This will return a logical matrix which will be true for each element if the relative difference between A and B with respect to B is less than 5 percent.
If you want to ask if all of these are true (they all satisfy the above condition):
all(tf(:))

Modifying Edric's solution:
absTol = 1e-3; % You choose this value to be what you want!
relTol = 0.05; % This one too!
absError = x(:)-y(:);
relError = absError./x(:);
relError(~isfinite(relError)) = 0; % Sets Inf and NaN to 0
same = all( (abs(absError) < absTol) & (abs(relError) < relTol) );
The variable same will be false if either the absolute or the relative error of any element is larger than whatever tolerances you choose. Also, if any elements of x happen to be exactly 0, then some of the elements of relError could end up being either infinite or not-a-number, so I used the ISFINITE function to ignore those values by setting them to 0.
I wouldn't suggest using IMAGESC to compare plots, since 1) the data is scaled when it is displayed, 2) the colormap for the display has a discrete number of color values (which I think is 256 by default, hence lots of rounding), and 3) subtle variations in color may not be all that apparent from visual comparison of two plots.

I would consider doing something like this with an absolute tolerance as well as a relative tolerance:
function same = tol( x, y )
absTol = 1e-3;
relTol = 0.05;
errVec = abs( x(:) - y(:) );
same = all( (errVec < absTol) | (errVec./x(:) < relTol) );

When you have very small value pairs in x and y, the result would return 0 although the values are ignorable themselves. So, an addition to the accepted solution
relError(x < absTol) = 0;
might be used to discard very small errors. Thus, the relative error is not considered for these values.

For matrices x and y containing floating point values, you may check if array elements are within a given tolerance of one another.
Sample code:
tol = 0.05;
result = abs(x - y) <= tol;

make use of 'isequal(a,b) where a and b are two matrices, if 1 it is true

Related

I am not able to compare two float number in MATLAB it is giving zero because of very small change how to avoid this

x=20;
LHS1=(sind(x)+cosd(x))^2
RHS1=1+2*sind(x)*cosd(x)
LHS1==RHS1
LHS2=(1-2*cosd(x)-3*(cosd(x))^2)/(sind(x))^2
RHS2=(1-3*cosd(x))/(1-cosd(x))
LHS2==RHS2
I am getting this answer as
LHS1 =
1.642787609686539
RHS1 =
1.642787609686539
ans =
1
LHS2 =
-30.163437477526365
RHS2 =
-30.163437477526383
ans =
0
That depends on how close you want the two values to be, to be considered "true". Can LHS2 and RHS2 be different by 0.1 and be considered equal? 0.001? 0.0000001? etc.
Decide on how close you want them to be, then check if the distance between the two are below some absolute amount.
threshold = 0.00001; % You decide this
difference = abs(LHS2 - RHS2); % Absolute distance between the two numbers
if (difference <= threshold)
% Do something
end
Note that you should not make the threshold too small, because computers have limits to how precise a decimal number can be (machine epsilon).
You may use ismembertol(x,y,tol) built-in function. In this case, x and y are your numbers while tol is your tolerance gap. You can make your tolerance really small to be able to sure about your equality.
For example,
x=1.000000001;
y=1;
tol=eps(1);
tol =
2.220446049250313e-16
ismembertol(x,y,tol)
ans =
logical
0
This means x and y are not equal.

Optimize nested for loop for calculating xcorr of matrix rows

I have 2 nested loops which do the following:
Get two rows of a matrix
Check if indices meet a condition or not
If they do: calculate xcorr between the two rows and put it into new vector
Find the index of the maximum value of sub vector and replace element of LAG matrix with this value
I dont know how I can speed this code up by vectorizing or otherwise.
b=size(data,1);
F=size(data,2);
LAG= zeros(b,b);
for i=1:b
for j=1:b
if j>i
x=data(i,:);
y=data(j,:);
d=xcorr(x,y);
d=d(:,F:(2*F)-1);
[M,I] = max(d);
LAG(i,j)=I-1;
d=xcorr(y,x);
d=d(:,F:(2*F)-1);
[M,I] = max(d);
LAG(j,i)=I-1;
end
end
end
First, a note on floating point precision...
You mention in a comment that your data contains the integers 0, 1, and 2. You would therefore expect a cross-correlation to give integer results. However, since the calculation is being done in double-precision, there appears to be some floating-point error introduced. This error can cause the results to be ever so slightly larger or smaller than integer values.
Since your calculations involve looking for the location of the maxima, then you could get slightly different results if there are repeated maximal integer values with added precision errors. For example, let's say you expect the value 10 to be the maximum and appear in indices 2 and 4 of a vector d. You might calculate d one way and get d(2) = 10 and d(4) = 10.00000000000001, with some added precision error. The maximum would therefore be located in index 4. If you use a different method to calculate d, you might get d(2) = 10 and d(4) = 9.99999999999999, with the error going in the opposite direction, causing the maximum to be located in index 2.
The solution? Round your cross-correlation data first:
d = round(xcorr(x, y));
This will eliminate the floating-point errors and give you the integer results you expect.
Now, on to the actual solutions...
Solution 1: Non-loop option
You can pass a matrix to xcorr and it will perform the cross-correlation for every pairwise combination of columns. Using this, you can forego your loops altogether like so:
d = round(xcorr(data.'));
[~, I] = max(d(F:(2*F)-1,:), [], 1);
LAG = reshape(I-1, b, b).';
Solution 2: Improved loop option
There are limits to how large data can be for the above solution, since it will produce large intermediate and output variables that can exceed the maximum array size available. In such a case for loops may be unavoidable, but you can improve upon the for-loop solution above. Specifically, you can compute the cross-correlation once for a pair (x, y), then just flip the result for the pair (y, x):
% Loop over rows:
for row = 1:b
% Loop over upper matrix triangle:
for col = (row+1):b
% Cross-correlation for upper triangle:
d = round(xcorr(data(row, :), data(col, :)));
[~, I] = max(d(:, F:(2*F)-1));
LAG(row, col) = I-1;
% Cross-correlation for lower triangle:
d = fliplr(d);
[~, I] = max(d(:, F:(2*F)-1));
LAG(col, row) = I-1;
end
end

how to define an array with fractional index number

Like suppose that I need to create a function named pressure denoted by p (a 2-D matrix) which depends on 2 variables r and z.
u, v, w are linear matrices which also depend on 2 variables r and z.
r and z are linear matrix defined below take i={1,2,3,4,5,6,7,8,9,10}
r(i)=i/10
z(i)=i/10
u(i) = 2*r(i) + 3*z(i)
v(i) = 8*r(i) + 4*z(i)
w(i) = 3*r(i) + 2*z(i)
p = p(r,z) %, which is given as,
p(r(i),z(j)) = 2*v(i) - 4*u(i) + w(j)
Now suppose the value of p at a given location (r,z) say (0.4,0.8) is needed, I want that if I give the input p(0.4,0.8), I get the result.
In your case the easiest way is to convert the fractional numbers to integers by multiplying by 10.
This way the location (r,z) = (0.4, 0.8) will become (4,8).
If you don't want to remember every time to provide the locations multiplied by 10, just create a function that will do it for you, so you can call the function with the fractional location.
If your matrices are linear, you will always find a multiplying factor to get rid of the fractional coordinates.
Not entirely sure what you mean here, but if your matrix is only defined in the indices you give (i.e. you only want to draw values from the fixed set of indices you defined), then this should do it:
% the query indices
r_i = 0.4;
z_i = 0.8;
value = p(r_i*10,z_i*10);
if you want to look at values between the ones you defined, you need to look at interpolation:
% the query indices
r_i = 0.46;
z_i = 0.84;
value = interp2(r,z, p, r_i, z_i);
(I may have gotten r and z in that last function in the wrong order, try it out).

MATLAB: I want to threshold a matrix, based on thresholds in a vector, without a for loop. Possible?

Let us say I have the following:
M = randn(10,20);
T = randn(1,20);
I would like to threshold each column of M, by each entry of T. For example, find all indicies of all elements of M(:,1) that are greater than T(1). Find all indicies of all elements in M(:,2) that are greater than T(2), etc etc.
Of course, I would like to do this without a for-loop. Is this possible?
You can use bsxfun like this:
I = bsxfun(#gt, M, T);
Then I will be a logcial matrix of size(M) with ones where M(:,i) > T(i).
You can use bsxfun to do things like this, but it may not be faster than a for loop (more below on this).
result = bsxfun(#gt,M,T)
This will do an element wise comparison and return you a logical matrix indicating the relationship governed by the first argument. I have posted code below to show the direct comparison, indicating that it does return what you are looking for.
%var declaration
M = randn(10,20);
T = randn(1,20);
% quick method
fastres = bsxfun(#gt,M,T);
% looping method
res = false(size(M));
for i = 1:length(T)
res(:,i) = M(:,i) > T(i);
end
% check to see if the two matrices are identical
isMatch = all(all(fastres == res))
This function is very powerful and can be used to help speed up processes, but keep in mind that it will only speed things up if there is a lot of data. There is a bit of background work that bsxfun must do, which can actually cause it to be slower.
I would only recommend using it if you have several thousand data points. Otherwise, the traditional for-loop will actually be faster. Try it out for yourself by changing the size of the M and T variables.
You can replicate the threshold vector and use matrix comparison:
s=size(M);
T2=repmat(T, s(1), 1);
M(M<T2)=0;
Indexes=find(M);

How to compute values closest to or equal to 0.5?

how to pick values from matrix closest to or equal to K = 0.5? I know I could obtain values from the matrix, by taking the absolute values and its min. However, I want to be able to loop through the matrix, check if the the first element is equal K, if it is equal,take its index and break. But if the first element is not equal to K, loop until you find value equal to K. Continue until all values equal to K is exhausted. Can anybody point me in the right direction? Thanks in advance.
Here is my code:
data = rand(10,2);k =0.5;
indr = find(data(:,1));
cNum = data(1,1);
if cNum < k
old_distance = abs(k - cNum);
else
old_distance = abs(cNum - k);
end
Xdata = data(2:end,:);
indeX = find(Xdata(:,1));
for i = 1:size(Xdata,1)
if Xdata(i,1) < k
min_Val = abs(k-Xdata(i,1));
new_distance = min(min_Val);
else
min_Val = abs(Xdata(i,1) -k);
new_distance = min(min_Val);
end
if (new_distance < old_distance)
old_distance = new_distance;
cNum = Xdata(i,1);
end
end
cNum_indeX = indr(indeXm);
Y = cNum;
X = indr(cNum_indeX);'
To find the closest value in a vector to a particular value you can do this:
>> data = rand(10, 1)
data =
0.7060
0.0318
0.2769
0.0462
0.0971
0.8235
0.6948
0.3171
0.9502
0.0344
>> k = 0.5;
>> [~, index] = min(abs(data - k));
>> closestValue = data(index)
closestValue =
0.3171
For loops are rarely the answer in MATLAB. Let's say you want to check if your array elements are within K ± tol, where tol is some tolerance that you've set. You can do that by simple logical indexing.
K=0.5;
tol=0.001; %# set your tolerance here
boolIndex=xVector<=K+tol & xVector>=K-tol; %# xVector is your vector
Now boolIndex is just a logical index array of 0's and 1's. It gives a 1 wherever your array element has satisfied this criteria. You can use this directly in indexing your vector for further manipulation. If, for some reason, you need the exact index, you can get them by doing find(boolIndex==1).
The constraints of the problem aren't clear enough (and I don't have enough points to make this a comment rather than an answer.)
Is speed critical here? If so, then you should avoid any sort of explicit loop. It's usually better to use builtin functions unless the matrix is really huge and you want to break when you find something close enough. If it's millions of entries long, I'd break it into chunks of 10000 or so and let MATLAB use the min function on chunks. Or rows. Or columns. Depends on what you want to do.
How close is close enough? You demonstrate with a random matrix, but are you expecting something within rounding error of 0.5?
Are you aware that [value,index]=min(x) will give the value and index of minimum?
I assume the matrix must be large, otherwise there would be no downside to letting MATLAB do the vectorized:
[colminval,colminind]=min(abs(x-0.5));
[minval,rowminind]=min(colminval);
That's the best I can do for direction without more... direction.