How Can I take 3d image from 3 matrix ( x,y,z) in one step ??

1 visualización (últimos 30 días)
Fatih
Fatih el 4 de Ag. de 2014
Respondida: Fatih el 5 de Ag. de 2014
function [mapBlock] = mapping (rwX,rwY,rwZ)
xMapBlock=single(200);
zMapBlock=single(100);
mapBlock = single(NaN(zMapBlock,xMapBlock));
[h w] = size(rwY);
for hh=1:1:h
for ww=1:1:w
if ~isnan(rwY(hh,ww))
if ~isnan(rwZ(hh,ww))
ix = ceil(rwX(hh,ww));
iz = ceil(rwZ(hh,ww));
mapBlock(iz,ix) = rwY(hh,ww);
end
end
end
end
end
I have this code for making 3D map but I dont want to use for loops because its so slow.I want to take code that can do it in one step .I bring rwX,rwY and rwZ , and this code get back with my 3d map. My goal is making 3d image. Thank you.

Respuestas (2)

Chris Turnes
Chris Turnes el 4 de Ag. de 2014
The key to removing the for loops from this code is to make use of a different kind of matrix indexing. In addition to row and column indices, you can also use linear indexing to access the elements of a matrix (see this documentation section for more details).
The loops in this code seem to be converting two of the input variables rwZ and rwX into row and column indices for the matrix mapBlock, respectively. By using linear indexing, the entries of mapBlock that are to be accessed and assigned can be determined in one vectorized operation.
Consider the following implementation of the function:
function [mapBlock] = mappingVec(rwX,rwY,rwZ)
% preallocate
mapBlock = nan(100,200,'single');
% find all non-nan entries
notNan = (~isnan(rwX)) & (~isnan(rwZ));
% convert inputs to linear indices
idx = sub2ind(size(mapBlock), ceil(rwZ(notNan)), ceil(rwX(notNan)));
% assign values
mapBlock(idx) = rwY(notNan);
end
This code is structured as follows:
  1. The mapBlock matrix is pre-allocated as a NaN matrix with class single.
  2. A logical variable called notNan is created to index the elements of rwX and rwZ that are not NaN valued. Element notNan(i,j) is true if both rwX(i,j) and rwZ(i,j) are not NaN and false otherwise. By indexing the matrices rwZ and rwX with notNan, only those entries for which notNan is true are accessed (see this documentation section on logical indexing for further explanation, and this documentation section on logical operations).
  3. The function subs2ind is then used to convert the index pairs (iz,ix) from the original code into linear indices.
  4. The corresponding values of rwY are assigned to mapBlock in the proper indices.
As a final note, in the original function the first if statement checked to see if rwY(hh,ww) was NaN. From the code structure, however, it seemed like this should have been rwX, so the vectorized version above contains this change.
  1 comentario
Fatih
Fatih el 5 de Ag. de 2014
thank you for your answer Chris. But I can not arrange size of mapBlock matrix. Because my first matrixes rwX rwY and rwZ are 64*50 sized. and if I take isnan from these matrix , new matrixes are aproximate 2000*1.and its not in sequence structured.for example 1 2 3 10 ....20 21 ....1 2 3 ....123. I can not handle that.

Iniciar sesión para comentar.


Fatih
Fatih el 5 de Ag. de 2014
maybe I can not describe what ı want, ı have 3 matrixes and I want to make map from these. This map must have 2 axis from rwX and rwZ , and rwy is going to be my depth information. you can consider like photo. only one difference. photo has x,y and lightining information. I want to change this lightining information with depth. Thank you.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by