How to concatenate the results of each iteration of a 'for' to create a final concatenated vector?

6 views (last 30 days)
I don't know if the title explained the objective of the question very well, however, the objective is to go concatenating each result of each iteration of my 'for' into a vector, which at the end will have 100 concatenations (a big vector).
My code uses the 'sum' function to scan each image (there are a total of 100 images and each one must be processed. Therefore, the for has 100 iterations) (I didn’t put the part of the code on reading each image and filtering them because it’s not the goal) of a flow containing bubbles as well as in the attached image. After the sum, i created a code to show the contours of the bubbles (1 to 1080 (image height)).
I tried in several ways to concatenate the results of the 'for' into an empty vector created in the first line of the code to add the results, however, without success. My question is how to concatenate the leftT vectors of each image into a single final vector.
empty = [];
for p = 1:100
scanning = sum(image, 2);
for i = 1:1080
if scanning(i)~=0
start = find(image(i,:),1,'first');
left(i)=start;
else
left(i)=342;
end
end
leftT = left.';
plot(leftT,'-b')
% I tried this, however, it didn't work:
% concatenated = [empty; leftT];
end
Any idea how i could do this?
Thanks in advance!
  2 Comments
Stephen23
Stephen23 on 29 Mar 2021
Original question by Jórdan Venâncio Leite on 23rd March 2021 retrieved from Google Cache:
How to concatenate the results of each iteration of a 'for' to create a final concatenated vector?
I don't know if the title explained the objective of the question very well, however, the objective is to go concatenating each result of each iteration of my 'for' into a vector, which at the end will have 100 concatenations (a big vector).
My code uses the 'sum' function to scan each image (there are a total of 100 images and each one must be processed. Therefore, the for has 100 iterations) (I didn’t put the part of the code on reading each image and filtering them because it’s not the goal) of a flow containing bubbles as well as in the attached image. After the sum, i created a code to show the contours of the bubbles (1 to 1080 (image height)).
I tried in several ways to concatenate the results of the 'for' into an empty vector created in the first line of the code to add the results, however, without success. My question is how to concatenate the leftT vectors of each image into a single final vector.
empty = [];
for p = 1:100
scanning = sum(image, 2);
for i = 1:1080
if scanning(i)~=0
start = find(image(i,:),1,'first');
left(i)=start;
else
left(i)=342;
end
end
leftT = left.';
plot(leftT,'-b')
% I tried this, however, it didn't work:
% concatenated = [empty; leftT];
end
Any idea how i could do this?
Thanks in advance!

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 23 Mar 2021
One option (that does not vary much from the posted code):
x{1} = [1 2 3 4 5];
x{2} = [6 7 8 9 10];
x{3} = [11 12 13 14 15];
concatenatedVector = cat(2,x{:})
producing:
concatenatedVector =
Columns 1 through 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Column 15
15
Other options are also available, depending on how the vector was created, and how it exists in its workspace.
  8 Comments
Walter Roberson
Walter Roberson on 25 Mar 2021
I did what I could, but those image files simply do not exist on my system, and it would be very unlikely that they would be in the matlabroot bin directory, which is only for the executables for MATLAB (might have an icon file or two, but no test data.) Are you sure the files are located there? If so which toolbox installed them?
for p = 1:3
pngarchivesname = sprintf('%d.png', p);
dinfo = dir(fullfile(matlabroot, '**', pngarchivesname));
if isempty(dinfo)
warning('No %s', pngarchivesname);
continue
end
found_one = false;
for K = 1 : length(dinfo)
completenamearchive = fullfile(dinfo(K).folder, dinfo(K).name);
image = imread(completenamearchive);
if size(image,1) < 650 || size(image,2) < 128
continue
end
found_one = true;
end
if ~found_one
warning('did not find any %s that were large enough', pngarchivesname);
continue;
else
fprintf('Yah! found %s\n', completenamarchive);
end
cropped = imcrop(image,[526.148 0 682.7 1080]);
if ndims(cropped) > 2
gray = rgb2gray(cropped);
else
gray = cropped;
end
threhsold = im2bw(gray, 0.7);
remove = bwareaopen(threhsold,3500);
se = strel('line',410,0);
closing = imclose(remove,se);
originalline = closing(1 , :);
originalline2 = closing(end , :);
closing(1, :) = true;
closing(end, :) = true;
filtered = imfill(closing, 'holes');
closing(end,:) = false;
closing(1,:) = false;
filtered(1, :) = originalline;
filtered(end, :) = originalline2;
scanning = sum(filtered, 2);
for i = 1:1080
if scanning(i)~=0
start = find(filtered(i,:),1,'first');
left(i)=start;
else
left(i)=342;
end
end
leftT = left.';
plot(leftT,'-b')
ConcatenatedVector = cat(1,leftT{:});
end
Stephen23
Stephen23 on 25 Mar 2021
Edited: Stephen23 on 25 Mar 2021
"Images must be in the "bin" directory."
Do NOT save data files to the BIN directory.
Do NOT save (or change) files to the installation directories of any application!
To start with, you should save data files somewhere under your user directory, for example in the default MATLAB startup folder (which happens to be exactly how MW Windows and MATLAB are designed to be used).
Apparently recent version of MS Windows protect the installation folders, hopefully that will go some way to preventing users placing image files in the BIN directrory.

Sign in to comment.


Walter Roberson
Walter Roberson on 25 Mar 2021
If the idea is to find the first non-zero element in each row, then you can vectorize
firsts = sum(cumprod(image == 0, 2),2)+1;
This code returns 1 more than the number of columns for the rows in which there are no non-zero values.
You can store this vector of results as columns, one column for each image, and then you can reshape() to get a single vector of locations, if that is what you want.

Categories

Find more on Image Processing Toolbox 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!