- the COLON in the FOR loop does not generate the entire vector (it basically generates the loop iterator using the step).
- the colon assigned to k generates the entire vector in memory using an algorithm that tries to ensure that the vector's first and last values match the COLON start and end values.
Problems in using map
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all, I have some problems using map in matlab. I don't understand why I get the error messages ""The specified key is not present in this container" after the 251th entry.
clear all;
k = -pi : 2*pi/500 : pi
delta_omega = -0.4: 0.008 : 0.4;
%Want to create a map
valueset_k = 1:1:(length(k));
valuesset_deltaomega= 1:1:length(delta_omega);
M_k = containers.Map(k,valueset_k);
M_deltaomega = containers.Map(delta_omega, valuesset_deltaomega);
%Want to construct an array to store the values
xi = [];
for p = -pi : 2*pi/500 : pi
xi(M_k(p)) = p^2;
end
%Want to construct a matrix to store the values
Matrix = [];
for k = -pi : 2*pi/500 : pi
for delta_omega = -0.4: 0.008 : 0.4
Matrix(M_k(k),M_deltaomega(delta_omega)) = k*delta_omega;
end
end
2 commentaires
Stephen23
le 23 Sep 2023
Modifié(e) : Stephen23
le 23 Sep 2023
You incorrectly assume that two different COLON calculations using binary floating point numbers will give results that are bit identical, but in fact they don't:
If you loop over k then you will avoid this error (but still have complex, fragile code).
It looks like you are trying to write MATLAB code using paradigms from some other language, because that code is just very obfuscated indexing: MATLAB already has indexing built in.
Note that your complex first loop with fragile dictionary simply reduces down to this:
xj = k.^2;
Réponse acceptée
Star Strider
le 23 Sep 2023
Modifié(e) : Star Strider
le 23 Sep 2023
You are seeing floating-point approximation error.
Changing the first loop slightly to check that:
kount = 0;
format long
for p = -pi : 2*pi/500 : pi
kount = kount+1;
if k(kount)-p ~= 0
Check = k(kount)-p
end
xi(M_k(p)) = p^2;
end
demonstrates this. Apparently MATLAB does not calculate ‘k’ and ‘p’ exactly the same way.
If you are not familiar with this chgaracteristic of floating-point numbers, see: Floating-Point Numbers for details.
clear all;
k = -pi : 2*pi/500 : pi;
delta_omega = -0.4: 0.008 : 0.4;
%Want to create a map
valueset_k = 1:1:(length(k));
valuesset_deltaomega= 1:1:length(delta_omega);
M_k = containers.Map(k,valueset_k);
M_deltaomega = containers.Map(delta_omega, valuesset_deltaomega);
%Want to construct an array to store the values
xi = [];
kount = 0;
format long
for p = -pi : 2*pi/500 : pi
kount = kount+1;
if k(kount)-p ~= 0
Check = k(kount)-p
end
xi(M_k(p)) = p^2;
end
%Want to construct a matrix to store the values
Matrix = [];
for k = -pi : 2*pi/500 : pi
for delta_omega = -0.4: 0.008 : 0.4
Matrix(M_k(k),M_deltaomega(delta_omega)) = k*delta_omega;
end
end
EDIT — (23 Sep 2023 at 14:46)
The easiest solution to this would likely be:
%Want to construct an array to store the values
xi = [];
for h = 1:numel(k)
p = k(h);
xi(M_k(p)) = p^2;
end
providing ‘k’ is still in your workspace (or stored somewhere that you can refer to it easily).
.
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!