2015-09-28 230 views
0

我想循环通过一些日志空间来计算Z值并创建一个轮廓图。但我被卡住了。我现在如何创建等值线图,我有我的Z值?我怎样才能设置我的Z变量?我对Z的计算是基于图像的,而我不能用其他方式来完成。MatLab - 循环通过X和Y来计算Z

X = logspace(-10,0,10); 
Y = logspace(-10,0,10); 
for x = X 
    for y = Y 
     % here should some magic happen... but you have to assign real positive integers as indices for z 
     z(x, y) = 1; % some other heavy calculation 
    end 
end 

% what should I do here? 
contourf(x, y, z); % does not work unfortunately 
+1

不是说你可能想要将事物计算为“X”和“Y”的函数。例如,你可以只说'Z = X.^3-2 * log(Y);'并从'X'和'Y'元素中计算'Z'。 –

+0

换句话说:如果可能的话,设计你的函数,使它在矩阵上运行!那么你不需要for循环,只需要写'Z = f(X,Y)',这通常更快。 – hbaderts

+0

的确如此,但是当为不同的X和Y值计算20个图像对的平均相关性时,它变得更加复杂。 –

回答

2

这是行得通吗?

X = logspace(-10,0,10); 
Y = logspace(-10,0,10); 
for x = 1:numel(X) 
    for y = 1:numel(Y) 
     %// Note the reversed y,x - this is because the x-axis in an image/chart is usually mapped to the horizontal axis which is the columns whereas matrix representations would have dimension one as the rows. Hence you need to put x in dimension 2 and y in dimension 1 
     z(y,x) = 1; %// i.e. z(y,x) = f(X(x), Y(y)) 
    end 
end 

contourf(X, Y, z); 
+0

是的,它差不多:)!它现在提出以下投诉:X的大小必须匹配Z的大小或Z的列数 。其中$ X $具有$ numel(X)= 2 $,$ Y $具有$ numel(Y) = 3 $和$ Z $有3列和2行。 –

+0

@Kevin尝试我的编辑 - 我只是在分配给'z'时转换'x'和'y' – Dan

+1

谢谢!有用 :)!现在我必须等待30分钟的结果:)。 –

相关问题