Create a loop to read a matrix
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hey guys, this is probably very easy, but im finding it difficult to do
I have a matrix surv_matrix, that assumes values of 0 and of a signal in frequency. this matrix only has values of a signal in frequency, along the column in specific columns. So imagine this matrix is 23979 by 400, and has values different from zero in column 200, 201 and 202. Along that 3 columns, this matrix assumes values to all the 23979 rows.
I want to identifie the columns that have values diferent from zero, and when it finds all the 3 columns do this code, for each column
plot(abs(freq_XQPSK),20*log10(abs(X_QPSK)),'b','linewidth',2);
xlim ([20e6 60e6]);
thanks a lot
採用された回答
dpb
2022 年 5 月 24 日
ic=find(~all(surv_matrix==0));
will return your three column indices to use to index into the array.
You haven't defined where the variables referred to in the plot command come frome...some operation over those three columns of the array, one presumes.
10 件のコメント
Miguel Albuquerque
2022 年 5 月 24 日
okay with that i have the indices where the matriz is not zero, but for each one of that indice I want to plot this:
plot(abs(freq_XQPSK),20*log10(abs(X_QPSK)),'b','linewidth',2);
xlim ([20e6 60e6]);
Miguel Albuquerque
2022 年 5 月 24 日
I want to red the matrix column by column
dpb
2022 年 5 月 24 日
"I want to re[a]d the matrix column by column..."
No. That is NOT the MATLAB way, use the column indices found above to address the array directly.
You still haven't DEFINED the variables in the plot command but if they are the same as the array you mentioned in the orginal posting, then
plot(abs(freq_XQPSK),20*log10(abs(X_QPSK(:,ic))),'b','linewidth',2)
The above assumes the three columns are all spectra and the frequency is an independent vector; adjust to fit the actual situation as needed -- we still don't have enough info to know just what is where in the original array nor how it relates to the above variables; this is just a guess of one possible way things COULD be.
Alternatively, simply for plotting, one could use the expedient of turning all zero-only columns into NaN and call plot() directly; it will automagically not plot the NaN values.
Or, (and probably really the better way) would be to simply write
ic=find(~all(surv_matrix==0));
surv_matrix=surv_matrix(:,ic);
to pull out the three columns of interest and deal with the much smaller array to start with.
Miguel Albuquerque
2022 年 5 月 24 日
okay I made it this way:
Basically freq_xqpsk is an array with 23979 elements that defines the frequency of a signal, X_QPSK is the magnitude of the signal. so I want to plot for each indice, the all column, what do you think of this way? thank you a lot
ic=find(~all(surv_matrix==0));
for idx=ic
surveillance_column=surv_matrix(:,idx)
plot(abs(freq_XQPSK),(abs(surveillance_column)),'b','linewidth',2);
hold on
continue
end
dpb
2022 年 5 月 24 日
"freq_xqpsk is an array with 23979 elements that defines the frequency of a signal,...."
OK, so my guess from earlier was pretty-much spot on. In that case I'd stick with my previous answer/sample code with the variables you've got defined. There's no need to use a loop here and that is not using MATLAB as it is designed/intended to be used. It is, after all, "MATrix LABoratory" and the builtin in functions such as plot are specifically designed to operate on arrays by column, so use the features of the language.
ic=find(~all(surv_matrix==0));
hL=plot(freq_XQPSK,abs(surv_matrix(:,ic)),'b','linewidth',2);
Or, as above, reduce the array to what's of interest in it before getting this far...
surv_matrix=surv_matrix(:,~all(surv_matrix==0)); % select non-zero spectra
hL=plot(freq_XQPSK,abs(surv_matrix),'b','linewidth',2); % plot against frequency
Either of those gives you the same result in much more efficient computational manner using MATLAB vectorized operations plus much less user code to write/debug/maintain.
I made the one assumption that your frequency is one-sided so abs is redundant; if it is indeed, two-sided, then the abs operation will flip the plot around the zero frequency location and give overlaying plots -- unless something has been done to those responses other than just FFT to compute them, then the two will be identical anyway.
NB: I saved the line handles of the lines when using plot, this lets you customize individual lines at will after having plotted; the colors will have cycled through the default sequence by column just as would/do with hold on and adding a line at a time.
Miguel Albuquerque
2022 年 5 月 24 日
In this case I have two-sided frequency, and Im only interested in positive frequencies . The plots will not be identical, since the frequencies values differ from column to column, because this is a radar mounted in a aeroplane, and this matrix corresponds to the movement of this plane, with doopler included.
Thanks
Miguel Albuquerque
2022 年 5 月 24 日
編集済み: Miguel Albuquerque
2022 年 5 月 24 日
With the code you gave me, I only see one plot, I think I need the for loop, or how can I edity the handle lines to see more than 1 plot, thank you again
dpb
2022 年 5 月 24 日
That's all you're going to get with your code, too...three lines on one plot; that's what the "hold on" is going to do.
If you want a separate plot for each, then that would, indeed, need a call to plot() for each column but it would also take a "figure" command in the loop to create the new figure/axis to plot into or you'll just replace the first with the next.
Miguel Albuquerque
2022 年 5 月 25 日
Thanks a lot man, your code was exactly what I wanted, I can see the separate lines in one plot.
But is there any way I can change the color of the lines and put a legend on it?
dpb
2022 年 5 月 25 日
That's why saved the line handles -- see <plot>, it gives example of changing after the line creation.
hL is an array of l ine handles; one for each line, use either one of the named basic eight colors or any RGB triplet for color.
<legend> is how legends are added -- simply write the text you want for each in sequence as they're drawn and it's done for the simplest case --
hLg=legend('Line A','Line B','Line C');
You can get as creative as you care to...use the doc to learn about all the properties -- there are many examples to guide the beginner...
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Get Started with MATLAB についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!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)
