index out of bounds

4 次查看(过去 30 天)
fang-chu
fang-chu 2013-7-12
%clc
clear
%parameters
dt = 1e-6;
B1 = 10;
phi = 0;
gamma = 267.513*10^6; %rad/(s*T)
Bzi = -5; %in HZ
Bzj = 5; %in HZ
a = 1000000;
G = 42.576; % Gyromagnetic ratio (MHz/T) MHZ=1*10^6
B_zi = Bzi/(a*G); %in tesla
B_zj = Bzj/(a*G); %in tesla
% mag_initial = [0 0 1; 0 0 1];
%functions
% equation magnetizaion = [m_x1 m_y1 m_z1; m_x2 m_y2 m_z2];
mag = zeros(2,3);
B = zeros(size(mag));
% line up
mag(:,3) = 1;
B(1,3) = B_zi;
B(2,3) = B_zj;
%time evolution
for nn = (1:10000)
% calculate B
M = mean(mag); %average of mag
m_x = M(1);
m_y = M(2);
euler = complex(cos(phi),sin(phi));
m = complex(m_x,m_y);
B0 = euler*m;
B_x = real(B0);
B_y = imag(B0);
B = [B_x B_y B_zi; B_x B_y B_zj]; % equation B = [B_x+B1 B_y B_zi; B_x+B1 B_y B_zj];
[tt, mm] = ode45('bloch',[0:dt/2:dt],mag,[],gamma,B);
mag = mm(:,3);
end
my function is
function dmdt = bloch(tspan,mag,spot,gamma,B) %outputs and inputs
mag = reshape(mag,2,3);
dmdt = gamma*cross(mag,B);
dmdt = dmdt(:);
return
the error I got
Attempted to access M(2); index out of bounds because numel(M)=1.
Error in main5 (line 32) m_y = M(2);
Error in run (line 57) evalin('caller', [s ';']);

回答(1 个)

Image Analyst
Image Analyst 2013-7-12
Looks like it should work. In fact it does for me. So I think your mean is not what you think it is. After you call mean(), put these lines:
which -all mean
whos M
What do you see?
  1 个评论
fang-chu
fang-chu 2013-7-13
编辑:fang-chu 2013-7-16
Name Size Bytes Class Attributes
M 1x3 24 double
Name Size Bytes Class Attributes
M 1x1 8 double
Attempted to access M(2); index out of bounds because numel(M)=1.
Error in main5 (line 34)
m_y = M(2);
this is what show after I run it
Do I have to add "if" comment? what do you mean my mean is not what I think it is?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by