Organizing Vectors into Structs then Structs into a Matrix
2 次查看(过去 30 天)
显示 更早的评论
Okay I'm working with some kind of confusing code so I'm going to do my best to explain things without copying the exact code I'm using.
I have a bunch of files I need to process which contain two related vectors, one vector is time (non linear) and the other vector contains amplitude values.
I'm trying to separate the vectors into structs of trials, each trial being 40 seconds long. (so any values between 0 and 40 seconds would be trial 1, any values between 41 and 81 would be trial 2.. etc)
My problem stems from writing the data into structs, if I have two values within one trial they get overwritten.
Example Vectors:
Time: 01, 05, 28, 43, 56, 69, 80, 102, 165
Amp: .0012, .005, .003, .456, .56, .098, .435, .454, .234
Example Code:
% Set up trial number and trial time
trialNum = 1;
trialTime = 40;
for i = 1: numel(files)
if time(i) <= trialTime;
trial(trialNum) = amp(i);
else
trialNum = trialNum +1;
trialTime = trialTime + 40;
trial(trialNum) = amp(i);
end
end
How can I change this so that it outputs things as a vector of values
For example with the given vectors the code would output:
trial(1) = [.0012,.005,.003]
trial(2) = [.456,.56,.098,.435]
trial(3) = [.435]
trial(4) =[0]
trial(5) = [.234]
0 个评论
采纳的回答
Walter Roberson
2014-3-5
binnum = 1 + floor(Time(:) / 40);
trial = accumarray( binnum, Amp(:), [], @(V) {V});
Note: 0 to 40 is 41 seconds not 40. The above code uses 0 to 40-eps, 40 to 79-eps and so on. If you want to be using 1 to 41-eps, 41 to 81-eps, and so on, then
binnum = 1 + floor( (Time(:)-1) / 40);
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!