Hi, I need some advice/help with cell accessing and operation in a for loop.
1 次查看(过去 30 天)
显示 更早的评论
So I have a "100x20001" matrix variable " i_w " which are the data points of 100 sine waves i.e. each row can map a sine wave. I have found the locations of the peaks (3 peaks would just be enough) and store them inorder to find the distance between 2 consecuitive peaks. i have used a cell in a for loop as below:
i=cell(100,2);
for n=1:100
% I have use findpeaks here to get the respective
% peaks and its respective location
[i{n,:},LOCS]=findpeaks(i_w(n,:),'NPeaks',3);
end
now this gave me a cell array such as
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195586/image.jpeg)
the locations of the first 3 peaks are stored in the second column.
now all i need to do is to access and find the difference between the 3rd location and 2nd location of each row until the 100th row and store them all.
eg:
751-418 = 333 and so
i have tried this, but seem to get an error which i cannot seem to understand:
dist=zeros(100,1);
for n=1:100
dist{n,:}=i{n,2}(3)-i{n,2}(2)
end
Index exceeds matrix dimensions.
Error in matrices (line 113)
dist{n,:}=i{n,2}(3)-i{n,2}(2)
so I know I have made a mistake but it would be a great help to hear some advice. Thanks in advance.
I am sorry the file "i_w" is too big to attach i will present the code instead:
t=0:0.001:20; % time
w=[0 3 6 9 12 15 18 21 24 0]; %frequency
a=[4 8 12 16 20 24 28 32 36 40]; % Amplitude
val=[sin(2*pi*w(1)*t);sin(2*pi*w(2)*t);sin(2*pi*w(3)*t);...
sin(2*pi*w(4)*t);sin(2*pi*w(5)*t);sin(2*pi*w(6)*t);...
sin(2*pi*w(7)*t);sin(2*pi*w(8)*t);sin(2*pi*w(9)*t);sin(2*pi*w(10)*t)];
out01_10=val*a(1);
out11_20=val*a(2);
out21_30=val*a(3);
out31_40=val*a(4);
out41_50=val*a(5);
out51_60=val*a(6);
out61_70=val*a(7);
out71_80=val*a(8);
out81_90=val*a(9);
out91_100=val*a(10);
i_w=[out01_10;out11_20;out21_30;out31_40;out41_50;...
out51_60;out61_70;out71_80;out81_90;out91_100];
2 个评论
采纳的回答
Star Strider
2018-11-18
The error is likely because of the first result being empty.
Try this:
for n=1:100
% I have use findpeaks here to get the respective
% peaks and its respective location
[i{n,1},LOCS]=findpeaks(i_w(n,:));
if ~isempty(LOCS)
peridx(:,n) = median(diff(LOCS,[],2)); % Use Either ‘median’ Or ‘mean’
end
end
Here ‘peridx’ is the median (or mean if you prefer) of the differences between the ‘LOCS’ index values for each row.
更多回答(1 个)
madhan ravi
2018-11-18
编辑:madhan ravi
2018-11-18
Since dist is a column vector use single index but you used double index which caused the problem :
dist{n}=i{n,1}-i{n,2} %column 1 subtracted by column 2
5 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!