MATLAB Answers

How do I store result from a loop into matrix?

15 views (last 30 days)
Syed Farez
Syed Farez on 6 May 2018
Commented: Syed Farez on 6 May 2018
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.
Syed Farez
Syed Farez on 6 May 2018
Yes Ameer, exactly like that brother. Sorry for the misguided question. I've corrected it.

Sign in to comment.

Accepted Answer

Wick on 6 May 2018
Edited: Wick on 6 May 2018
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.
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);
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)
Syed Farez
Syed Farez on 6 May 2018
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. :')

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!