MATLAB Answers

## Create an Array of vectors within a for loop

Asked by Nick Costner

### Nick Costner (view profile)

on 19 Nov 2019 at 10:47
Latest activity Commented on by Nick Costner

### Nick Costner (view profile)

on 19 Nov 2019 at 22:09
Accepted Answer by James Tursa

### James Tursa (view profile)

Hello
I am a student, i was given a vocal signal of 5000x1 which i need to get it's power spectral density using pwelch function. The frequency of the signal changes over time . So i was told to cut it into 50 segments of 100 points and use the pwelch function on each of these segments.So that at the end i can plot the frequencies i got from every segment as a function of time.
The issue i get is in the syntax , i tried this :
for j=0:100:5000;
i=[1:50];
[PXX(i),F(i)]=pwelch(data(1+j:100+j),20,4,2^13,1000);
end
but i get this as a result "In an assignment A(I) = B, the number of elements in B and I must be the same."
I know that the output of the pwelch function are two column vectors of nx1 size.What i want to do is to create 50 elements of nx1,only i dont know how to write it.

#### 0 Comments

Sign in to comment.

## 1 Answer

### James Tursa (view profile)

Answer by James Tursa

### James Tursa (view profile)

on 19 Nov 2019 at 18:52
Edited by James Tursa

### James Tursa (view profile)

on 19 Nov 2019 at 18:53
Accepted Answer

If you want to store the column vectors, you could use cell arrays. E.g.,
k = 1;
for j=0:100:5000;
:
[PXX{k},F{k}]=pwelch(data(1+j:100+j),20,4,2^13,1000);
k = k + 1;
Then downstream in your code you can use the curly brace { } syntax to get at the column data.

Nick Costner

### Nick Costner (view profile)

on 19 Nov 2019 at 22:09
Thanks, it worked . Only i had to put a 'if' condition for the k.Because it exceeds the matrix dimensions.

Sign in to comment.