saving values of one array into another array using for loop statement
이전 댓글 표시
It may be little complicated to explain for me this but I'm trying my best, if not quite clear then please ask me again for clarification. So, I have some 3-4 array to process in this problem. First array is 'centroids' (mx2 double). My second array is 'point3D' (437x517x3 single). Now using the values of 'centroids' I want to find out the corresponding values of 'point3D' and store these values in another array named 'co_ord'. For example say in 'centroids' for X1 and Y1, I want to extract corresponding values in 'point3D' and so on upto Xm and Ym and store them into 'point3D' for further use. I got 'centroids' array through Image processing and 'point3D' array from stereo vision.
Code which I tried is as
load('C:\Users\Naseeb\Desktop\centroids.mat'); % load centroids file
load('C:\Users\Naseeb\Desktop\point3D.mat'); %load point3D file
centroids = ceil(centroids); % change values into integer
x = cell(size(centroids,1),1); % create array X for storing first column values
y = cell(size(centroids,1),1); % create array Y for storing second column values
i = cell(size(centroids,1),1);
for i = 1:size(centroids,1)
for row = 1:size(centroids,1)
for column = 1:size(centroids,2)
x{i} = centroids(size(centroids,1),1); % store values of first column into array X
y{i} = centroids(size(centroids,1),2); % store values of second column into array Y
co_ord = point3D(x{i},y{i},:); % store values into co_ord file
end
end
end
I tried in this way, you can suggest me direct way to extract values of 'point3D' from 'centroids' and save them in another array 'co_ord'.
Thanks
채택된 답변
추가 답변 (1개)
Naseeb Gill
2016년 1월 11일
댓글 수: 2
Note that in general nested loops are a slow way of coding in MATLAB, and vectorized code (as per my answer) is a usually the faster, neater and more efficient option. This is why I wrote vectorized code for you.
Also note that you do not preallocate the arrays Co_ordinates and temp, and expand these inside loops. A much more memory-efficient solution would be to allocate those arrays before the loops and use indexing to allocate into them.
For more tips on writing efficient MATLAB code read this:
Naseeb Gill
2016년 1월 12일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!