saving values of one array into another array using for loop statement

9 views (last 30 days)
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
  2 Comments
Stephen23
Stephen23 on 5 Jan 2016
Edited: Stephen23 on 5 Jan 2016
Please upload your data files: without them it is difficult for us to run your code or try any changes to it. You need to click the paperclip button, then both the Choose file and Attach file buttons.
Naseeb Gill
Naseeb Gill on 5 Jan 2016
Hello Stephen, I attached both files to my question as you asked and I'm attaching those files with this also.
Thanks.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 6 Jan 2016
Edited: Stephen23 on 7 Jan 2016
Try this:
% load the data:
matc = load('C:\Users\Naseeb\Desktop\centroids.mat');
matp = load('C:\Users\Naseeb\Desktop\point3D.mat');
ctrd = ceil(matc.centroids);
pt3D = matp.point3D;
% get the indices:
N = size(pt3D,3);
X = repmat(ctrd(:,1),1,N);
[Y,Z] = ndgrid(ctrd(:,2),1:N);
I = sub2ind(size(pt3D),X,Y,Z);
% extract the required data points:
out = pt3D(I);
where each row of out is one of the rows of centroids:
>> out
out =
-0.13502 -0.35063 1.3853
-0.044997 0.30485 1.2942
-0.55011 0.85439 1.9908
The variable out contains the values of point3D at the index positions given in centroids, with the rows corresponding between out and centroids. Note that MATLAB is optimized for indexing, not for-loops, so I used much faster linear indexing to get obtain all of the values at once rather than using a slow and inefficient for-loop. Also note that it is recommended to load data into variables, rather than simply into the workspace. This allows tracking of the variables, prevents variables being overwritten without warning, and makes debugging easier.

More Answers (1)

Naseeb Gill
Naseeb Gill on 11 Jan 2016
I also tried to solve above problem myself and get the following solution.
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
null=[];
Co_ordinates=[]; % final matrix where Co_ordinates will be stored
for step=1:3;
temp=[];
for k=1:length(centroids);
null=point3D(centroids(k,1),centroids(k,2),step);
temp=[temp null];
end
Co_ordinates=[Co_ordinates; temp];
step=step+1;
end
  2 Comments
Stephen23
Stephen23 on 11 Jan 2016
Edited: Stephen23 on 11 Jan 2016
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
Naseeb Gill on 12 Jan 2016
Thanks Stephen for your valuable suggestions and I'm using your code only. I just tried from my side also and I get above solution. So, I just put this code also here.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!