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.
Related
I have inequalities with two unknown variables. So how could I assume one variable with different values and get the others?
For instance: -15<10*x+2*y<20.
How could I assume x=2, 3, and so on, and then find answer of (y) depending on the value of (x)?
I have been trying to apply the assume and find commands, but unfortunately, I could not. So I hope anyone could help me, please.
Looking forward to hearing from you.
I am new to Matlab, so I have been trying to apply solve, assume, and find commands
clear all;
clc;
syms x y real;
z=solve(-15<10*x+2*y,[x y])
b=solve(10*x+2*y<20,[x y])
yinterval = [ z,b]
I expect the output: to assume x=different numbers and then y= be a list of possible results depending on the value of x
Thanks,
For each value of x, technically there are infinite values of y that satisfy those equations, so for my solution, I assumed x and y were integer values. As well, it appears that you want to give the program a set of x values and have it calculate y values for each x value. Instead of using the solve command, we can simply use a couple of loops to find all satisfactory integer values of y for each value of x.
To start, we need to make a results matrix to store each x,y pair that satisfies the equations you've given. This is called pre-allocation, as we're pre-allocating the space needed to store our answers. Using the equations, we can deduce that there will be 17 satisfactory y values per x. So, our first two lines of code will be initializing the desired x-values and the results matrix:
xVec = 1:5; %x-vector, change this to whatever x-values you want to test
results = zeros(length(xVec)*14, 2); %results matrix
Note: If you decide to iterate x or y by a value different than +1 (more on that later), you'll need to come up with a different method of creating this results matrix. You could also just not pre-allocate the results matrix, but your code will run slower as the size of the results matrix will be changing on each loop.
Next are the loops. Admittedly, this is not the most elegant solution, but it'll get the job done. First, we need an index to keep up with where we are in our results matrix. This is pretty easy, we'll just call it index and start at 1 (since MATLAB indexes from 1 in matrices. Remember that!):
index = 1; %index for results matrix
Next, we need to loop through each value in our x-vector. Simply use a for loop:
for x = xVec
...
For each value of x, there is a minimum value of y. This value can be solved for in
-15 < 10*x + 2*y --> -14 = 10*x + 2*y_min
So, simply solving for y gives us our next line of code:
y = -7 - 5*x; %solving for y
Note: each time we iterate x in our for loop, a new starting value of y will be calculated.
Finally, we need to loop through values of y that still satisfy the inequalities given. This is performed through use of a while loop:
while 10*x + 2*y > -15 && 10*x + 2*y < 20
...
Note: && is the 'and' statement while using loops. You can't use a single equation for this (i.e. you can't say something like -15 < x < 20, you have to split them up using &&).
Since we solved for the first value of y, we can go ahead and record the current x and y values in our results matrix:
results(index, :) = [x, y]; %storing current x- and y-values
Then, we need to iterate y, as otherwise we'd be stuck in this while-loop forever.
y = y + 1;
Note: You can iterate this y-value with whatever amount you want. I chose to iterate by 1 each time, as I assumed you wanted to find integer values. Just change the +1 to whatever value you want.
Finally, we iterate our index, so that the next pair of x,y values that satisfy our equations don't overwrite our previous solutions.
index = index + 1;
All that's left is to close our loops and run! As I said, this isn't the most efficient solution, so I wouldn't use this for large amounts of x- and y-values. As well, like with iterating the y-values, the x-values can have any 'step-size' you want. As it's coded currently, it jumps +1 between each x, but changing the xVec input to any vector will still work (ex. xVec = 1:0.1:5; iterates the x-value by +0.1 each step instead of +1).
Here's the code all together, sans comments (since I wrote the comments while making the above code snippets):
xVec = 1:5;
results = zeros(length(xVec)*14, 2);
index = 1;
for x = xVec
y = -7 - 5*x;
while 10*x + 2*y > -15 && 10*x + 2*y < 20
results(index, :) = [x, y];
y = y + 1;
index = index + 1;
end
end
Let me know if you have any questions!
I would like to generate an array which contains all ordered samples of length k taken from a set of n elements {a_1,...,a_n}, that is all the k-tuples (x_1,...,x_k) where each x_j can be any of the a_i (repetition of elements is allowed), and whose total number is n^k.
Is there a built-in function in Matlab to obtain it?
I have tried to write a code that iteratively uses the datasample function, but I couldn't get what desired so far.
An alternative way to get all the tuples is based on k-base integer representation.
If you take the k-base representation of all integers from 0 to n^k - 1, it gives you all possible set of k indexes, knowing that these indexes start at 0.
Now, implementing this idea is quite straightforward. You can use dec2base if k is lower than 10:
X = A(dec2base(0:(n^k-1), k)-'0'+1));
For k between 10 and 36, you can still use dec2base but you must take care of letters as there is a gap in ordinal codes between '9' and 'A':
X = A(dec2base(0:(n^k-1), k)-'0'+1));
X(X>=17) = X(X>=17)-7;
Above 36, you must use a custom made code for retrieving the representation of the integer, like this one. But IMO you may not need this as 2^36 is quite huge.
What you are looking for is ndgrid: it generates the grid elements in any dimension.
In the case k is fixed at the moment of coding, get all indexes of all elements a this way:
[X_1, ..., X_k] = ndgrid(1:n);
Then build the matrix X from vector A:
X = [A(X_1(:)), ..., A(X_k(:))];
If k is a parameter, my advice would be to look at the code of ndgrid and adapt it in a new function so that the output is a matrix of values instead of storing them in varargout.
What about this solution, I don't know if it's as fast as yours, but do you think is correct?
function Y = ordsampwithrep(X,K)
%ordsampwithrep Ordered samples with replacement
% Generates an array Y containing in its rows all ordered samples with
% replacement of length K with elements of vector X
X = X(:);
nX = length(X);
Y = zeros(nX^K,K);
Y(1,:) = datasample(X,K)';
k = 2;
while k < nX^K +1
temprow = datasample(X,K)';
%checknew = find (temprow == Y(1:k-1,:));
if not(ismember(temprow,Y(1:k-1,:),'rows'))
Y(k,:) = temprow;
k = k+1;
end
end
end
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
if i have a given array, say:
a = 0.1333
0.2667
0.0667
0.5333
and i want to find the value that is the closest to, but still less than:
b = 0.29
what is the best way to do this? the way I have been doing it so far is:
% add extra column for indices and sort by values
A = [(1:length(a))', a];
A = sortrows(A, 2);
% iterate along value column of A
for ii = 1:length(A)
if A(ii,2) < b
continue
else
ii = ii - 1;
break
end
end
% get corresponding value from index column in this case 2
idx = A(ii,1);
this gives me the outcome i am looking for, however it feels a bit "hacky" and not at all "matlabby".. can someone please suggest how i can improve this code?
This solution assumes that elements of a are always positive.
If no elements smaller than t is found the smallest element of the array is returned instead as desired.
a = [0.1333 0.2667 0.0667 0.5333];
t = 0.29;
% a2 is a copy of a but where elements bigger or equal to t are set to opposite value
a2 = a;
a2(a >= t) = -a2(a >= t);
% find closest to 't' in 'a' that are below 't' - that is the maximum of the elements of a2
[~, idx] = max(a2);
result = a(idx);
% print result
disp(result)
disp(idx)
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