Finding a value in a specific row within a matrix (c) closest to 700. Then telling me what column this value is situated.

1 visualización (últimos 30 días)
So far my attempts:
round(i700) % This whole number is used to search a specific row ie) round(i700) = row(i)
for i=round(i700) % so it searches only for that row
for j=1:M % searches across all columns for that row
[vals]=abs(c(round(i700),j)-700); % calcs every value in the row - 700
MinIndexes = find(vals == min(vals)); % finds the smallest value and locates its index
[row, col] =ind2sub(size(c),[MinIndexes]); % finds that index value in the original matrix (c) and tells me the row and column
end
end

Respuestas (1)

DGM
DGM el 23 de Abr. de 2021
What's wrong with just doing this?
A = magic(10) % dummy test matrix
row = 4; % look in this row
findthisvalue = 21 % for this number
col = find(A(row,:)==findthisvalue);
numberlocation = [row col]
gives
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
findthisvalue =
21
numberlocation =
4 4
  2 comentarios
Alex Hinchcliffe
Alex Hinchcliffe el 23 de Abr. de 2021
Editada: Alex Hinchcliffe el 23 de Abr. de 2021
Thanks for the reply.
This looks good but may need an extra line or two incase the row has no exact value to say ie. 21. So it needs to find the nearest to that value in the row.
Edit: This is what i found to work.
row = round(i700)
[~, col] = min(abs(c(row,:) - 700))
DGM
DGM el 23 de Abr. de 2021
Oh. Yeah, for some reason I missed the "closest" part of the problem description.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by