Store different size arrays in a matrix according to index in a loop
2 views (last 30 days)
Show older comments
Hey,
Im using blim toolbox which creates lines (y*x matrix where x can have different sizes) stored in a 10x1 cell, every cell corresponding to one line. I have a dataset of more then 5000 files, each file with a 10x1 cell. I created a loop where it reads one cell for every file giving me the vector x and y of the correspondent line. I want to store the y values in a matrix where the y value is in the right x position, using the x vector as index for the position of y values. And filling the empty values with NaN.
My loop is:
for ifile = 1:size(imgfilenames,2) % process each file in the list
imgfilename = imgfilenames{ifile};
blimfilename = getentryname(blimdir, imgfilename, blimext); % get the blimline filename
% load line 1 from blimline files
[message blimlines blimsettings] = loadblimline(blimdir, blimfilename, line_1);
if ~isempty(blimlines{line_1}) % if this blimline is not empty
blimx = blimlines{line_1}(1,:); % horizontal coordinates (pixels!)
blimy = blimlines{line_1}(2,:); % vertical coordinates (pixels!)
end
end
I created a basic example where I'm trying to obtaining the following result in my matrix:
47 61 62 52 66 64 51 5 29 53 12 15 68 15 74
NaN NaN NaN NaN NaN NaN NaN NaN NaN 49 3 59 59 77 18
21 6 58 32 66 41 36 NaN NaN NaN NaN NaN NaN NaN NaN
using:
a = [1:15; rand(1,15)];
b = [10:1:15; rand(1,6)];
c = [1:7; rand(1,7)];
abc = {a,b,c};
matrix = zeros(3,15);
for i = 1:3;
x = abc{i}(1,:); % horizontal coordinates (pixels!)
y = abc{i}(2,:); % vertical coordinates (pixels!)
end
I already tried padcat and padadd but it doesn't work inside my loop since the vectors are generated in every interaction of the loop.
Thanks in advance for helping me out!
2 Comments
Answers (1)
Guillaume
on 22 Apr 2016
Since you know maximum x you can predeclare your final array:
maxx = 1601;
out = nan(1, maxx);
Then it's just a matter of simple indexing to fill that vector:
for ifile = 1:numel(imgfilename) % process each file in the list
imgfilename = imgfilenames{ifile};
blimfilename = getentryname(blimdir, imgfilename, blimext); % get the blimline filename
% load line 1 from blimline files
[message, blimlines, blimsettings] = loadblimline(blimdir, blimfilename, line_1);
if ~isempty(blimlines{line_1}) % if this blimline is not empty
blimx = blimlines{line_1}(1,:); % horizontal coordinates (pixels!)
blimy = blimlines{line_1}(2,:); % vertical coordinates (pixels!)
out(blimx) = blimy;
end
end
2 Comments
Guillaume
on 22 Apr 2016
I recommend you use numel instead of length. It's safer as numel will work even if imgfilenames is a matrix.
out = nan(numel(imgfilename), maxx);
for ...
...
if ...
out(ifile, blimx) = blimy;
end
end
See Also
Categories
Find more on Matrices and Arrays 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!