Problems in using map
古いコメントを表示
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 件のコメント
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
2023 年 9 月 23 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!