【问题标题】:How to cut up the value of the array in matlab based variable input如何在基于matlab的变量输入中切割数组的值
【发布时间】:2015-02-07 05:47:21
【问题描述】:

如果我有一个 1xN 值的数组,其中值是从数组变量 xy 和 z 的值派生的,如何根据这样的点值 x 的值分割数组的值包含值数组编号 y以及 x 值的结果。

xplot=[0];
for x=0:1:12;
   for y=0:0.5:5;
      for z=0:1:5;

         f=2.*x+3.*y+4.*z;

      end
   end
 end

xplot=[xplot;f]

我想根据z的值来划分数组的值

xplotz0=[]
xplotz1=[]
 .
 .
 .
xplotzn=[]

因为最终结果我想添加所有这些数组

xzplottot = xplotz0 + xplotz1 + ... + xplotzn


如果有一个临时存储模型的数组和收集器数组,那么每个 z 的值,数组都会被馈送到收集器数组然后自动进入收集器数组...其中数组的值将连续添加作为z的值y的个数。

【问题讨论】:

  • 尝试使用ndgridmesh 并避免循环。
  • 在你当前的代码中,f 不是一个数组,它是一个标量,每次迭代都会重新计算。

标签: arrays matlab function for-loop plot


【解决方案1】:

怎么样

[x y z] = meshgrid( 0:1:12, 0:0.5:5, 0:1:5 )
f = 2.*x+3.*y+4.*z; %//all at once - no need for nested loop.

现在 f 是一个 3D 数组,您可以通过索引最后一个暗淡来访问不同 z 值的 f

f(:,:,1) 

为您提供z=0 的所有f

【讨论】:

  • :) 和 xzplottot = sum(f,3)
【解决方案2】:
  XTable=[];
  for x=0:1:12;
  for y=0:0.5:5;
  z=0:1:5;    
  f=2.*x+3.*y+4.*z;
  XTable=[XTable f'];        
  end
  end
  XTableZ0=sum(XTable(1,:));
  XTableZ1=sum(XTable(2,:));

等等。

=============

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2014-10-30
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多