Confusion in matrix multiplication
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Dear all, the magnitude of the second graph does not make any sence: why is it so low?
These are absolutely same vectors, but the second graph is just truncated to half of the first one.The vector "frequency_domain" is real-valued, the vector "spectrum_normalized" is complex-valued.
Could you please help me to fix it?
Thank you in advance!
UPD: spectrum_normalized was just a matrix, not a vector, due to a typo.... Check dimensions first.

採用された回答
Voss
2024 年 11 月 25 日
spectrum_normalized is a matrix (I know that because subplot(2,4,2) contains many lines). Thus, to plot the one-sided spectrum, you'd plot the first N/2 rows or the first N/2 columns, not the first N/2 elements, i.e.:
plot(frequency_domain(1:N/2), abs(spectrum_normalized(1:N/2,:)));
% or
plot(frequency_domain(1:N/2), abs(spectrum_normalized(:,1:N/2)));
% but not
plot(frequency_domain(1:N/2), abs(spectrum_normalized(1:N/2)));
depending on whether each row or each column of spectrum_normalized corresponds to an element of the vector frequency_domain.
7 件のコメント
Sergei
2024 年 11 月 25 日
Thank you, thant helped! I am so glad because i was trying to find the mistake for a lot of time<3
Could you please tell, why so or where to find this information? Thank you again!
also if i create a vector one_sided_spectrum = spectrum_normalized(1:N/2) how then to plot this signal?
plot(frequency_domain(1:N/2), abs(one_sided_spectrum(1:end,:))); does not work
Voss
2024 年 11 月 25 日
"why so or where to find this information?"
The following page has information about array indexing. The section "Linear Indexing" explains what happens when you index a matrix with only one subscript (as in the expression spectrum_normalized(1:N/2)).
"if i create a vector one_sided_spectrum = spectrum_normalized(1:N/2)"
spectrum_normalized(1:N/2) is a vector consisting of the first N/2 elements of spectrum_normalized.
Now, I don't know how or whether that vector corresponds to frequency_domain because I don't know whether the spectra are stored as rows or columns of spectrum_normalized.
If each column of spectrum_normalized is a spectrum (i.e., each row corresponds to one frequency value in frequency_domain), then spectrum_normalized(1:N/2) is the first half of the first spectrum (i.e., it's the same as spectrum_normalized(1:N/2,1). If this were the case, the plot command you showed would work and produce something meaningful.
However, if each row of spectrum_normalized is a spectrum (i.e., each column corresponds to one frequency value in frequency_domain), then spectrum_normalized(1:N/2) is the first value (i.e., the value corresponding to the first frequency) of each of the first N/2 spectra (assuming spectrum_normalized has at least N/2 rows - and if not, then it includes samples from the second and possibly subsequent values of each spectra). If this were the case, the plot command you showed would run but produce incoherent nonsense. Since you say the plot command didn't work, I guess this is the situation you have. So in that case, to plot the first half of all spectra:
plot(frequency_domain(1:N/2), abs(one_sided_spectrum(:,1:N/2)))
or to plot the first half of some particular spectra:
to_plot = [1 3 10]; % select the spectra to plot
plot(frequency_domain(1:N/2), abs(one_sided_spectrum(to_plot,1:N/2)))
The thing is spectrum_normalized is not a matrix, it is a vector that obtained by a line spectrum_normalized = fft(signal)/N, where "signal" is a vector of size N.
The lines you provided in the first answer solved the problem: the difference in order in values dissappeared and it looks beautiful now. But for me it does not make sence as spectrum_normalized is a vector, not a matrix, as I understand. Could you please explain how you came up with the lines in your first answer
If spectrum_normalized is a vector and not a matrix, then
plot(frequency_domain, abs(spectrum_normalized));
would've produced one plotted line in subplot(2,4,2).
But in fact multiple lines exist in that subplot, which indicates either (1) spectrum_normalized is a matrix in which case the plot command creates multiple lines, or (2) you've done some plotting into that subplot axes using code not shown in the question.
You can check the size of spectrum_normalized in right before you plot it in your code, by running
whos spectrum_normalized
or
size(spectrum_normalized)
immediately before subplot(2,4,2).
I figured it out! the problem was that spectrum_normalized was a matrix, not a vector, as you have said. It was due to a transposed by mistake matrix in multiplication by which spectrum_normalized is obtained.
Thank you for your time and attention to my question! Sorry for the confusion. Checking the dimensions matters, I consider it as a lesson...
Voss
2024 年 12 月 4 日
You're welcome! Good to know it's sorted out.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
