フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by