MATLAB Answers

finding an closest possible element in an 7 dimensional matrix array

2 views (last 30 days)
Ganesh Kini
Ganesh Kini on 18 Apr 2020
Commented: Ganesh Kini on 28 May 2020
Code is as follows
time = period_fun(2,2,1,10,10,15,3) is a 7D matrix with has domensions 2*7*1*10*10*15*8
So when hardcode the time = 26.601 and pass it.
I am trying to find the closest value possible in period_arr
%finding the closest element possible to time = 26.601
dist = abs(period_fun - time);
min_dist = min(dist(:));
idx = find(dist == min_dist );
The output comes as 26.601, which is wrong.
My array has a value 26.801 which is closer to 26.601 it is not able to pick that value.
How can i precisely tune it ? so that i can make it more robust for even 0.001 variation
Please help me out


Show 1 older comment
Ganesh Kini
Ganesh Kini on 18 Apr 2020
min_dist = min (dist (:));
I tried with this too.
My matrix does not have 26,601 as the value. I just gave a random number with decimals to check the precision. I have tried for 4-5 examples its not working
It is not able to trace the nearest value possible
Please suggest

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 18 Apr 2020
A matrix with dimensions [2 7 1 10 10 15 8], will have 168000 elements. The file you shared only have 166379 elements. I cannot create the matrix with a specified dimension. If I suggest a solution, you may still not find that it is not working. However, I have given a solution by padding array with zeros to make its size equal to 168000.
fid = fopen('Mat file.txt');
data = textscan(fid, '%f', 'HeaderLines', 6);
period_arr = data{1};
period_arr = padarray(period_arr, 168000 - numel(period_arr), 0, 'post');
period_arr = reshape(period_arr, [2 7 1 10 10 15 8]);
time = 26.601;
dist = abs(period_arr - time);
[min_dist, idx] = min(dist(:));
When I run this code, it get the value
Which is closest value to 26.601.
26.801 is not the closest value in this array.

  1 Comment

Ganesh Kini
Ganesh Kini on 28 May 2020
Hi Ameer,
Thanks for the suggestion i have an issue.
1 ´ = 2.3
t2 = 5.6
% its is a 2 * 7 * 1 * 10 * 10 * 15 * 8 matrix
p = period_arr (1,:,:,:,:,:, :); % this is of the form p = period_arr (p1, p2, p3, p4, p5, p6, p7);
n = period_arr (2,:,:,:,:,:, :); % this is of the form n = period_arr (n1, n2, n3, n4, n5, n6, n7);
dist_p = abs (p - t1);
[min_dist_p, idx_p] = min (dist_p (:));
dist_n = abs (n - t2);
[min_dist_n, idx_n] = min (dist_n (:));
c_tp = p (idx_p);
c_tn = n (idx_n);
So based on the minimum distance i get the closest value, and it is working fine.
But, I have a problem here I have to get only the closest value where the indices p4 = n4 and p5 = n5. It should regulate the same value for both p and n.
How do i do that? please help me out. I am stuck in this problem from 2 days
For example
I can have
2.6 = period_arr (1,:,:, a, b,:, :)
7.8 = period_arr (2,:,:, a, b,:, :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 18 Apr 2020
If you have the Statistics and Machine Learning Toolbox, try knnsearch(). I think you can have every point be a new class. It will tell you which point is closest to your query point.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!