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 个评论
Walter Roberson
2016-11-1
We need to know length(I)
HYEONG JIN KIM
2016-11-1
Walter Roberson
2016-11-1
You need to tell us size(I) for us to be able to debug your problem.
HYEONG JIN KIM
2016-11-1
回答(2 个)
Walter Roberson
2016-11-1
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 个评论
HYEONG JIN KIM
2016-11-1
Walter Roberson
2016-11-1
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
2016-11-1
HYEONG JIN KIM
2016-11-1
Walter Roberson
2016-11-1
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
2022-1-2
0 个投票
Can you pls provide solution for this giving error as data type
6 个评论
Torsten
2022-1-2
v is undefined in your script.
Kodiganti Saikrishna
2022-1-2
Iam attaching my slx file with this. I need frequency response plot Can you pls help me
Kodiganti Saikrishna
2022-1-2
This is simulink model
Image Analyst
2022-1-2
What does this say
>> whos v
Kodiganti Saikrishna
2022-1-3
V is workspace variable name A signal is generated in time domain i want to convert is as frequency spectra
Image Analyst
2022-1-3
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!