필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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
Stephen23
Stephen23 2016년 1월 5일
편집: Stephen23 2016년 1월 5일
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 2016년 1월 5일
Hello Stephen, I attached both files to my question as you asked and I'm attaching those files with this also.
Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 1월 6일
편집: Stephen23 2016년 1월 7일
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.

추가 답변 (1개)

Naseeb Gill
Naseeb Gill 2016년 1월 11일
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
Stephen23
Stephen23 2016년 1월 11일
편집: Stephen23 2016년 1월 11일
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 2016년 1월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by