Index exceeds matrix dimensions

fs =1e7; % Sample Rate
lengthofData =length(v);
nPower2 = 2 ^ nextpow2(lengthofData); % next closest power of 2 to the length
lengthofData1 =length(I);
nPower22 = 2 ^ nextpow2(lengthofData1);
freqRange = 0.5*fs*linspace(0,1,nPower2 / 2+1); % Frequency range % Only plotting upto n/2
v1=fft(v,nPower2)/lengthofData;%fast fourier transform
I1=fft(I,nPower22)/lengthofData1;%fast fourier transform
sys1=abs(v1); %absoulute value
sys2=abs(I1);
phi1=angle(v1); %phase angle value
phi2=angle(I1);
fresult1=sys2(:,1:1000001)./sys1; %magnitude output/input
fresult2=phi2(:,1:1000001)/phi1; %phase output.input <- error happens here!
plot(freqRange,20*log10(fresult1(1:N/2+1)),'r'); %magnitude transfer function = 20log10(output/input)
plot(freqRange,20*log10(fresult2(1:N/2+1)),'b'); %phase transfer function = 20log10(output/input)
Variable 'v' and 'I' were exported from Simulink.
As shown in image, I've tried to two variable division. However, it said 'index exceeds matrix dimensions'.
What can I do for this error? Please help me. Thank you

4 Commenti

We need to know length(I)
Actually, the variable 'v' and 'I" were from simulink workspace so I just named the length of v and I..
You need to tell us size(I) for us to be able to debug your problem.
they are (1:1000001) double

Accedi per commentare.

Risposte (2)

Your code does not get errors about index exceeding matrix dimensions for me; instead it gets errors about matrix dimension must agree.
You have
fresult1=sys2(:,1:1000001)./sys1; %magnitude output/input
fresult2=phi2(:,1:1000001)/phi1; %phase output.input <- error happens here!
Here, sys1 is a vector of length nPower2 and sys2 is length nPower22 . You extract the first 1000001 columns of sys2 but you divide it by the full nPower2 columns of sys1.
In the second command, you have similar disagreement on lengths, but the situation is a bit different because you are not using the same division operator as the previous line. You are not using element-by-element division, you are using algebraic matrix division, which is like
phi2(:,1:1000001) * pinv(phi1)
In order for the / operator to work, it is different dimensions that must be equal compared to the ./ operator.
I suspect you want to use ./ instead of / after you fix the length difficulties.

5 Commenti

The reason i added the (1:1000001) was to make same length between sys2 and sys1. sys2 was more longer than sys1 which had maximum 1000001. To make same length, I added the code. what is the meaning of *pinv() ?
pinv() Moore-Penrose pseudoinverse of matrix
Your sys1 is not maximum length 1000001. Your sys1 is abs(v1) and v1 is fft(v,nPower2)/lengthofData which is going to be length nPower2 . As you created nPower2 = 2 ^ nextpow2(lengthofData) then v1 is going to be a power of 2 long and so sys1 would be a power of 2 long. Your input data lengths for one of the vectors would have to be no more than about 500000 for your sys1 and sys2 to disagree on length .
then.. which part do i need to edit and how can i change the code?
As i changed the length and everything,, it still showed same error...
assert(nPower2 == nPower22); %if this is not true then you need to rethink your formulas.
fresult1=sys2./sys1; %magnitude output/input
fresult2=phi2./phi1; %phase output.input
In the plot step after that, you will need to define N before you can plot.

Accedi per commentare.

Kodiganti  Saikrishna
Kodiganti Saikrishna il 2 Gen 2022

0 voti

Can you pls provide solution for this giving error as data type

6 Commenti

v is undefined in your script.
Iam attaching my slx file with this. I need frequency response plot Can you pls help me
This is simulink model
What does this say
>> whos v
V is workspace variable name A signal is generated in time domain i want to convert is as frequency spectra
OK, so you didn't do what I said and enter that, "whos v", on the command line. Or if you did, you didn't report what it said.
Anyway, your question is not an Answer for @HYEONG JIN KIM so please start your own question rather than "Answering" a 5 year old question.

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Richiesto:

il 1 Nov 2016

Commentato:

il 3 Gen 2022

Community Treasure Hunt

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

Start Hunting!

Translated by