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

11 ビュー (過去 30 日間)
Jórdan Venâncio Leite 2021 年 3 月 23 日
コメント済み: Rena Berman 2021 年 5 月 6 日 18:51
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?
##### 2 件のコメント表示非表示 1 件の古いコメント
Rena Berman 2021 年 5 月 6 日 18:51

サインインしてコメントする。

### 回答 (2 件)

Star Strider 2021 年 3 月 23 日
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 件のコメント表示非表示 7 件の古いコメント
Stephen Cobeldick 2021 年 3 月 25 日
"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.

サインインしてコメントする。

Walter Roberson 2021 年 3 月 25 日
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.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by