Store different size arrays in a matrix according to index in a loop

3 views (last 30 days)
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
Sara Antonio
Sara Antonio on 22 Apr 2016
Edited: Sara Antonio on 22 Apr 2016
yes, it is the maximum number of pixels of the image on where the lines are created. In this case is 1601.

Sign in to comment.

Answers (1)

Guillaume
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
Sara Antonio
Sara Antonio on 22 Apr 2016
Thank you for helping me, but it didn't worked. The output was the last interection of the loop instead of saving all values. I changed the out matrix for the size of my final output:
time = length(imgfilenames); maxx = 1601; out = nan(time, maxx); (it is a 5551*1601 matrix)
What i'm trying to do is store every y array in each row in the right x position. Can't i specify inside the loop that I want to save every result like out(ifile) like i would do it if every result was the same size?
Guillaume
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!