Organize data as vector matrix and do calculation ?

1 回表示 (過去 30 日間)
Yu-Chen
Yu-Chen 2013 年 5 月 30 日
I want to organize the data returned by sph2cart as a matrix with vector element and all the element in matrix will perform some calculation(vector-vector or vector-scalar calculation) with a vector. Here is an example i achieve this however it's somewhat redundant, how could i make it more terse ? Thanks.
lightV = zeros(1, 1, 3);
lightV(1,1,1) = 0.5;
lightV(1,1,2) = 0.4;
lightV(1,1,3) = 0.7;
[Az El] = meshgrid(0:60:360, 0:15:90);
[x y z] = sph2cart(Az*pi/180, El*pi/180, 1);
refV = zeros(size(Az,1), size(Az,2), 3);
radius = zeros(size(Az,1), size(Az,2));
for i = 1:size(Az,1)
for j = 1:size(Az,2)
refV(i,j,1) = -x(i,j);
refV(i,j,2) = -y(i,j);
refV(i,j,3) = z(i,j);
radius(i,j) = dot(refV(i,j,:), lightV(1,1,:));
end
end

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 5 月 30 日
編集済み: Andrei Bobrov 2013 年 5 月 30 日
[Az, El] = meshgrid(0:60:360, 0:15:90);
[x, y, z] = sph2cart(Az*pi/180, El*pi/180, 1);
refV2 = cat(3,-x,-y,z);
lightV = [.5 .4 .7];
radius = sum(bsxfun(@times,refV,reshape(lightV,1,1,[])),3);
or
radius = reshape([-x(:), -y(:), z(:)]*lightV.',size(x));

その他の回答 (2 件)

Iman Ansari
Iman Ansari 2013 年 5 月 30 日
lightV = zeros(1, 1, 3);
lightV(1,1,1) = 0.5;
lightV(1,1,2) = 0.4;
lightV(1,1,3) = 0.7;
[Az El] = meshgrid(0:60:360, 0:15:90);
[x y z] = sph2cart(Az*pi/180, El*pi/180, 1);
refV=cat(3,-x,-y,z);
radius= lightV(1,1,1)*-x+lightV(1,1,2)*-y+lightV(1,1,3)*z

David Sanchez
David Sanchez 2013 年 5 月 30 日
a couple of changes:
% you know the values from the start and no need of 3D matrix
lightV = [.5 .4 .7];
[Az El] = meshgrid(0:60:360, 0:15:90);
[x y z] = sph2cart(Az*pi/180, El*pi/180, 1);
refV = zeros(size(Az,1), size(Az,2), 3);
radius = zeros(size(Az,1), size(Az,2));
for i = 1:size(Az,1)
for j = 1:size(Az,2)
refV(i,j,1) = -x(i,j);
refV(i,j,2) = -y(i,j);
refV(i,j,3) = z(i,j);
ref = reshape(refV(i,j,:),1,3); % reshape to 2D matrix
radius(i,j) = dot(ref, lightV(1,:));
end
end

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by