Reducing a vector size when it is being updated in a loop

Hi,
I have a vector A that is being updated in a loop, A contains data from a Trial, and the length of it changes with each iteration. I want to make the length of each A the same so the plots are of the same size.
Trial 1 length = 497 x 12, Trial 2 length = 700 x 12 ... Trial 5 length = 687x12 ( I want to get a 497x12 vector for all trials)
I currently have this piece of code, which find the smallest length of the A - the problem is the line of code that the smallest length out is inside the loop and after each iteration it changes.
The smallest number is a hint to how long the array A should be, since it varies for each trial uploaded.
I hope the below code helps
smallest = 10000;
for h=1:5
%code to load all script files
if h==1
A = dlmread("Trial 1");
elseif h==2
A = dlmread("Trial 2");
elseif h==3
A = dlmread("Trial 3");
elseif h==4
A= dlmread("Trial 4");
elseif h==5
A= dlmread("Trial 5");
end
[x , y] = size(A);
if x<smallest
smallest = x; %the smallest length of rows for A
end
...
end

 采纳的回答

for h=5:-1:1
%code to load all script files
if h==1
A = dlmread("Trial 1");
elseif h==2
A = dlmread("Trial 2");
elseif h==3
A = dlmread("Trial 3");
elseif h==4
A= dlmread("Trial 4");
elseif h==5
A= dlmread("Trial 5");
end
Acell{h}=A;
end
smallest = min( cellfun(@(c) size(c,1), Acell) );
Ashort=cellfun(@(c)c(1:smallest,:) , Acell,'uni',0);
for i=1:numel(Ashort)
figure(i); plot(Ashort{i});
end

4 个评论

Thanks for your answer, quite new to MATLAB - what does Acell{h}=A do
Also, when I incoporated to my code, it gave an error
Undefined function or variable 'Acell'.
Error (line 26)
xsmallest= min( cellfun(@(c) size(c,1), Acell) );
what does Acell{h}=A do
It stores A in Acell{h}.
Undefined function or variable 'Acell'.
Shouldn't happen. Did you copy/paste my code? In any case try re-pasting.
Thanks the code works well, inorder to extrapolate the data from the cell so I can use them in my calculations should I use the function celltomat?
No, just grab whichever data set you need by indexing, as in Acell{i}.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by