Index exceeds matrix dimensions
Show older comments
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 Comments
Walter Roberson
on 1 Nov 2016
We need to know length(I)
HYEONG JIN KIM
on 1 Nov 2016
Walter Roberson
on 1 Nov 2016
You need to tell us size(I) for us to be able to debug your problem.
HYEONG JIN KIM
on 1 Nov 2016
Answers (2)
Walter Roberson
on 1 Nov 2016
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 Comments
HYEONG JIN KIM
on 1 Nov 2016
Walter Roberson
on 1 Nov 2016
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 .
HYEONG JIN KIM
on 1 Nov 2016
HYEONG JIN KIM
on 1 Nov 2016
Walter Roberson
on 1 Nov 2016
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.
Kodiganti Saikrishna
on 2 Jan 2022
0 votes
Can you pls provide solution for this giving error as data type
6 Comments
Torsten
on 2 Jan 2022
v is undefined in your script.
Kodiganti Saikrishna
on 2 Jan 2022
Iam attaching my slx file with this. I need frequency response plot Can you pls help me
Kodiganti Saikrishna
on 2 Jan 2022
This is simulink model
Image Analyst
on 2 Jan 2022
What does this say
>> whos v
Kodiganti Saikrishna
on 3 Jan 2022
V is workspace variable name A signal is generated in time domain i want to convert is as frequency spectra
Image Analyst
on 3 Jan 2022
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.
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!