简体   繁体   中英

how to find close value of matrix using matlab

supposed i have a data contains a matrix

a = [ 401.2 405.5 408.6 411.9 415.33 418.6 422.04 425.39; 0.011 0.022 0.033 0.044 0.055 0.066 0.077 0.088]
b = [ 400;
      410;
      420];

is it possible to create matrix of C that looks like this, where the closest value of B on A will be put on C.

c = [400 410 420; 0.011 0.44 0.77]

for example, the value on B is 400, and the closest value of A is 401.2, then in matrix C, i will put 401.2 and 0.011.

thank you

Try this. The answer I get for c does not match the matrix you have given though.

a = [ 401.2 405.5 408.6 411.9 415.33 418.6 422.04 425.39; 
      0.011 0.022 0.033 0.044 0.055 0.066 0.077 0.088]
b = [ 400;
      410;
      420];

c = zeros(2,length(b));

for i = 1:length(b)
   [min_error, ind] = min(abs(a(1,:) - b(i)));
   c(:,i) = [b(i); a(2,ind)];
end

Here's the correct c matrix:

c = [400.0000,  410.0000,  420.0000;
     0.0110,      0.0330,    0.0660];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM