How do I store result from a loop into matrix?

I have 360 signals to be processed and I'm using STFT for my project. The result for one signal returns:
S = 33x147 double
How do I take only the first column from every 360 signals processed and store it in the same matrix? And how do I make a loop to make it do the same thing but advance to the next column up to the 147th column until I've 147 different matrices of result S?
Thank you in advance.

2 件のコメント

Ameer Hamza
Ameer Hamza 2018 年 5 月 6 日
Does your result require 147 matrices at the end? Like this
Result{1} = [first columns of S for all 360 signals] = 33 x 360 matrix
Result{2} = [second columns of S for all 360 signals] = 33 x 360 matrix
....
Result{147} = [147th columns of S for all 360 signals] = 33 x 360 matrix
Syed Farez
Syed Farez 2018 年 5 月 6 日
編集済み: Syed Farez 2018 年 5 月 6 日
Yes Ameer, exactly like that brother. Sorry for the misguided question. I've corrected it.

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

 採用された回答

Wick
Wick 2018 年 5 月 6 日
編集済み: Wick 2018 年 5 月 6 日

1 投票

The first column of S can be indexed S(:,1). The nth is S(:,n);
Instead of making 360 different matrices with different names, we're going to make a three-dimensional array, A, with 360 different pages.
clearvars
num_rows = 33;
num_signals = 360;
num_pages = 147;
A = zeros(num_results, num_signals, num_pages);
for jj = 1:num_signals
% replace the following line with your calculation of S
S = rand(num_results,num_pages);
A(:,jj,:) = reshape(S,num_results,1,num_pages);
end
Each page of A (the different columns from your values of S pasted together into a matrix) can be indexed as:
page_of_columns = A(:,:,n)
where 'n' is the column id you want (from 1 to 147 in your example)

7 件のコメント

Syed Farez
Syed Farez 2018 年 5 月 6 日
For that num_results, what do I initialize it with? Or is that the num_rows?
Wick
Wick 2018 年 5 月 6 日
編集済み: Wick 2018 年 5 月 6 日
num_rows is the 33 in the size(S) = [33 147] in your problem statement.
Edit: There were 3 values described in your problem statement. [33 147] was the size of each S. Those are num_rows and num_pages respectively. You also mentioned you had 360 such signals. That's num_signals.
I didn't hard code the numbers in case you wanted to change the number of signals, or the amount of data per S.
Syed Farez
Syed Farez 2018 年 5 月 6 日
Here's my code. I toned it down a little bit after the mat. file load as it is a bit lengthy. Basically, my S values will come from the 20*log. But if I'm gonna use your suggestion earlier, does the S from the "rand" replaced the result from the calculation?
clearvars
num_rows = 33;
num_signals = 360;
num_pages = 147;
num_results = 1;
A = zeros(num_results, num_signals, num_pages);
for jj = 1:num_signals
% load a .mat file
load ('Defect_Signals_with_Labels.mat');
x = Signals_Combined(:,jj);
.....
S = 20*log10(S + 1e-6);
S = rand(num_results,num_pages);
A(:,jj,:) = reshape(S,num_results,1,num_pages);
end
Wick
Wick 2018 年 5 月 6 日
You wouldn't put the 'load' command inside the 'for' loop. There's no need to load it 360 times (unless there are 360 different files to load...).
My 'rand' command was just something to make a 33x147 matrix to work with. You should replace that line with each value of S for each of the 360 signals one at a time.
Syed Farez
Syed Farez 2018 年 5 月 6 日
I don't quite understand on the replacing part actually. Could you show me on the code please Sir? :)
Wick
Wick 2018 年 5 月 6 日
編集済み: Wick 2018 年 5 月 6 日
clearvars
num_rows = 33;
num_signals = 360;
num_pages = 147;
num_results = 1;
A = zeros(num_results, num_signals, num_pages);
% load a .mat file
load ('Defect_Signals_with_Labels.mat');
for jj = 1:num_signals
x = Signals_Combined(:,jj);
.....
S = 20*log10(S + 1e-6);
A(:,jj,:) = reshape(S,num_results,1,num_pages);
end
The assumption is that 'S' is different each time based on whatever is above it inside the loop.
Syed Farez
Syed Farez 2018 年 5 月 6 日
Okay. I think I've got it now and it worked like a charm. Just needed a bit troubleshooting. Thank you very much for your help!! You certainly saved me today Sir. :')

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

その他の回答 (0 件)

カテゴリ

質問済み:

2018 年 5 月 6 日

コメント済み:

2018 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by