首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Matlab中正确实现矢量化

如何在Matlab中正确实现矢量化
EN

Stack Overflow用户
提问于 2013-02-28 02:29:54
回答 1查看 118关注 0票数 0

我有一组代码需要很长时间才能运行。我仔细阅读了mathworks网站上的矢量化页面。我还是有点困惑,有没有可能对我运行plane_intersect的部分进行矢量化?

未矢量化

代码语言:javascript
复制
for N = 1:sizeDimages
    imPos = anaInfoSat(N).ImagePositionPatient;
    A2Z = imPos(3);
    A2Y = imPos(2);
    A2X = imPos(1);

    [upP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[uppersatX1,uppersatY1,uppersatZ1],crossS,[A2X,A2Y,A2Z]);
    [loP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[lowersatX1,lowersatY1,lowersatZ1],crossS,[A2X,A2Y,A2Z]);
end

我尝试向量化,事实是upP1是一个Nx3矩阵。我预先分配了upP1矩阵。下面的代码返回关于维度不匹配的错误。ImagePosition是1x3矩阵。

代码语言:javascript
复制
N = 1:sizeDimages;
imPos = anaInfoSat(N).ImagePositionPatient;
A2Z = imPos(3);
A2Y = imPos(2);
A2X = imPos(1);

[upP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[uppersatX1,uppersatY1,uppersatZ1],crossS,[A2X,A2Y,A2Z]);
[loP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[lowersatX1,lowersatY1,lowersatZ1],crossS,[A2X,A2Y,A2Z]);

以下是plane_intersect代码的一部分,应该足以让您了解它的作用。

代码语言:javascript
复制
function [P,N,check]=plane_intersect(N1,A1,N2,A2)
%plane_intersect computes the intersection of two planes(if any) 
% Inputs: 
%       N1: normal vector to Plane 1
%       A1: any point that belongs to Plane 1
%       N2: normal vector to Plane 2
%       A2: any point that belongs to Plane 2
%
%Outputs:
%   P    is a point that lies on the interection straight line.
%   N    is the direction vector of the straight line
% check is an integer (0:Plane 1 and Plane 2 are parallel' 
%                              1:Plane 1 and Plane 2 coincide
%                              2:Plane 1 and Plane 2 intersect)
%
% Example:
% Determine the intersection of these two planes:
% 2x - 5y + 3z = 12 and 3x + 4y - 3z = 6
% The first plane is represented by the normal vector N1=[2 -5 3]
% and any arbitrary point that lies on the plane, ex: A1=[0 0 4]
% The second plane is represented by the normal vector N2=[3 4 -3]
% and any arbitrary point that lies on the plane, ex: A2=[0 0 -2]
%[P,N,check]=plane_intersect([2 -5 3],[0 0 4],[3 4 -3],[0 0 -2]);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-28 02:52:57

在您的矢量化代码中,anaInfoSat(N).ImagePositionPatient;不会返回单个答案,而是返回多个答案。如果将结果赋给单个变量,它将只收到第一个答案。这就是你得到尺寸不匹配错误的原因。

根据数据类的不同,您可以组合到一个矩阵中

代码语言:javascript
复制
imPos = [anaInfoSat(N).ImagePositionPatient];

或放入单元格阵列

代码语言:javascript
复制
imPos = {anaInfoSat(N).ImagePositionPatient};

您还可以同时为多个变量赋值:

代码语言:javascript
复制
[A2X, A2Y, A2Z] = anaInfoSat(1:3).ImagePositionPatient;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15119549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档