Store arrays of different size in each for loop itertaion
49 views (last 30 days)
Show older comments
Turbulence Analysis
on 10 Apr 2024 at 21:25
Hi,
In the below script, I need to store the output kkkk into a new array but in the same column. Here, size of the kkkk is different during each itertaion.
I know that cell array would solve the issue. But I need to store them in the single colum.
for i=1:4
kkkk = find(A>=sz(i,1) & A<=sz(i,1)+100);
clear kkkk
end
1 Comment
Accepted Answer
Matt J
on 10 Apr 2024 at 23:07
kkk=cell(4,1);
for i=1:4
kkkk{i} = find(A>=sz(i,1) & A<=sz(i,1)+100);
end
kkkk=vertcat(kkkk{:});
5 Comments
Matt J
on 11 Apr 2024 at 14:00
I corrected the varibale name and preallocated the cell array correctly. Still the processing time is very large.
If you profile the code, I think you will see that the bottleneck is coming from find(), not from anything else. It's an expensive function.
More Answers (1)
Matt J
on 11 Apr 2024 at 14:03
Edited: Matt J
on 11 Apr 2024 at 14:05
b=sparse(sz(:,1)');
kkkk = nonzeros( (A(:)>=b & A(:)<=b+100).*(1:numel(A))' );
1 Comment
Turbulence Analysis
on 11 Apr 2024 at 15:18
Edited: Matt J
on 11 Apr 2024 at 15:56
See Also
Categories
Find more on Matrix Indexing 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!