Append multiple values in one variable
이전 댓글 표시
hello,
i read an image using
img=imread('612.jpg')
then convert this 512x512 matrix into one column
using
newimg=img(:);
this give me a single column value 786432x1
know i want to insert new picture value started from 786433 and proceed next..
This experiment apply for 500 pictures..
답변 (1개)
the cyclist
2023년 3월 18일
Assuming the image are all the same size, 512x512, then I would "stack" them in the 3rd dimension, then convert everything to a vector at once. You don't mention how the variables are stored, the naming convention, etc, but it would go something like this
% Number of images
N = 500;
% Preallocate the 3d array
img_array = zeros(512,512,N);
% Loop to fill each "slice" in 3d
for ni = 1:N
img_array(:,:,ni) = ... % Here is where you need to figure out how to access each image
end
% Convert the whole thing to a vector. (This will be in the same order as if you had stacked them individual.)
img_vector = img_array(:);
댓글 수: 7
Sara Ejaz
2023년 3월 19일
the cyclist
2023년 3월 19일
You used this code to read one image:
img=imread('612.jpg')
Now you want to read hundreds of images, right?
But you haven't told us how those images are stored, what the naming convention is for the folders, or the files. So, it is not possible for us to help you with that, because we don't have the information.
The code I posted only solved what you asked for, which is how to "Append multiple values in one variable".
I suggest you open an new question, something like "How to read multiple files from a folder?" and give all the details about how your files are stored.
Sara Ejaz
2023년 3월 19일
the cyclist
2023년 3월 19일
Use the dir command to get a list of the files, then use a for loop to iterate over the files and append then like I showed.
Sara Ejaz
2023년 3월 19일
Sara Ejaz
2023년 3월 19일
the cyclist
2023년 3월 19일
% The number of files you have
N = 500;
% Length of each file
L = 786432;
% Preallocate the memory for the large file.
out = zeros(L,N);
% Loop over the input files, and store them
for ni = 1:N
filename = sprintf("%d.csv",ni); % This read 1.csv, 2.csv, 3.csv, etc, up to N.csv
out(:,ni) = readmatrix(filename);
end
% Write output as one long vector
writematrix(out(:),"long.csv");
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!