필터 지우기
필터 지우기

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

조회 수: 7 (최근 30일)
Sara Antonio
Sara Antonio 2016년 4월 22일
댓글: Guillaume 2016년 4월 22일
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
Guillaume
Guillaume 2016년 4월 22일
Do you know the maximum x?
Sara Antonio
Sara Antonio 2016년 4월 22일
편집: Sara Antonio 2016년 4월 22일
yes, it is the maximum number of pixels of the image on where the lines are created. In this case is 1601.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2016년 4월 22일
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
Sara Antonio
Sara Antonio 2016년 4월 22일
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 2016년 4월 22일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by