Problems in using map

1 回表示 (過去 30 日間)
Tsz Tsun
Tsz Tsun 2023 年 9 月 23 日
コメント済み: Star Strider 2023 年 9 月 23 日
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
k = 1×501
-3.1416 -3.1290 -3.1165 -3.1039 -3.0913 -3.0788 -3.0662 -3.0536 -3.0411 -3.0285 -3.0159 -3.0034 -2.9908 -2.9782 -2.9657 -2.9531 -2.9405 -2.9280 -2.9154 -2.9028 -2.8903 -2.8777 -2.8651 -2.8526 -2.8400 -2.8274 -2.8149 -2.8023 -2.7897 -2.7772
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
Error using indexing
The specified key is not present in this container.
%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 件のコメント
Stephen23
Stephen23 2023 年 9 月 23 日
編集済み: Stephen23 2023 年 9 月 23 日
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:
  • 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.
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;
Tsz Tsun
Tsz Tsun 2023 年 9 月 23 日
Thank you very much for pointing out the error, I loop over k and the problem is solved now.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2023 年 9 月 23 日
編集済み: Star Strider 2023 年 9 月 23 日
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
Check =
-4.440892098500626e-16
Error using indexing
The specified key is not present in this container.
%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 件のコメント
Tsz Tsun
Tsz Tsun 2023 年 9 月 23 日
Thank you very much for spotting the error detailing the break down of the problem, my problem is solved now using for loop again to define the k array.
Star Strider
Star Strider 2023 年 9 月 23 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by