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

 Respuesta aceptada

dpb
dpb el 24 de Mayo de 2022
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 comentarios

Miguel Albuquerque
Miguel Albuquerque el 24 de Mayo de 2022
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
Miguel Albuquerque el 24 de Mayo de 2022
I want to red the matrix column by column
dpb
dpb el 24 de Mayo de 2022
"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)
is all you need. <plot> plots an array by column by default.
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.
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
dpb el 24 de Mayo de 2022
"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
Miguel Albuquerque el 24 de Mayo de 2022
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
Miguel Albuquerque el 24 de Mayo de 2022
Editada: Miguel Albuquerque el 24 de Mayo de 2022
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
dpb el 24 de Mayo de 2022
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
Miguel Albuquerque el 25 de Mayo de 2022
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
dpb el 25 de Mayo de 2022
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...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 24 de Mayo de 2022

Comentada:

dpb
el 25 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by