MATLAB "Out of memory"问题

    首先,我要声明,matlab自带的Help才是最权威的Matlab学习资料,如果有时间好好学习一下或是可以高效的使用的话,一定受益匪浅!比如说像Out of Memory这个问题,最开始我都是用Help memory,几乎得不到任何信息;然后就是去网上搜索此类问题的解决方法,一般有这几种:

    除了升级内存和升级64位系统外,下面几个方法也是解决之道。

# 增加虚拟内存  %%试了,不行
# 采用PACK (在命令行输入 pack 整理内存空间) %% pack命令不能用在程序中
# 采用3GB 开关启动系统(修改 c盘根目录 boot.ini 启动选项加上 /3G 例如:multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /noexecute=optin /fastdetect /3G

                       %% 试了, 不行
# 优化程序,减少变量 (使用稀疏矩阵 sparse ) save 保存变量 load 变量,需要时再读出来
# 如果必有必要,不要启动java虚拟机,采用matlab -nojvm启动(在快捷方式属性里的 "..../matlab.exe")改为("...../matlab.exe" - nojvm)
#关闭Matlab Server 
# 使用 单精度 single 短整数替代 双精度。 %% 这个管用!!!!!!MATLAB <wbr>"Out <wbr>of <wbr>memory"问题(zz)

    现在看来这些解决方法对于我的问题来说根本就是“治标不治本”,不能解决实际问题.重申――Matlab的Help才是Matlab的最权威的指导材料!!!
    其实我的所谓原创就是对Help out of memory的一些归纳总结而已,有兴趣的话大家可以自己去看!!!
问题一:Matlab是如何存储矩阵的
    Matlab中矩阵是以Block,也就是块的形式存储的。也就是说,当Matlab在为即将存储的矩阵划分块时,如果没有相应大小的连续内存,即使实际内存没有被完全使用,他还是会报告“Out of Memory”。

问题二:如何高效使用Memory
    由于在使用的过程中,由于存储单元的不断的被分配和清除,内存会被分割成不连续的区域,这是很容易造成“Out of Memory”。

1.为矩阵变量预制内存而不是动态分配
    动态分配的过程中,由于开始Matlab所用的Block随着矩阵的增大而连续的为此矩阵分配内存,但是由于Block的不连续性,很有可能最开始分配的Block不能满足存储的需要,Matlab只好移动此Block以找到更大的Block来存储,这样在移动的过程中不但占用了大量的时间,而且很有可能它找不到更大的块,导致Out of Memory。而当你为矩阵变量预制内存时,Matlab会在计算开始前一次性找到最合适的Block,此时就不用为变量连续的分配内存。
    比较下面两个程序:

for k = 2:1000

x(k) = x(k-1) + 5;
end

x = zeros(1, 1000);
for k = 2:1000

x(k) = x(k-1) + 5;
end

    显然,第二个更好!!!最好的方法是,在程序一开始就位所有大的矩阵变量预制存存储单元!!!
    尽量早的分配大的矩阵变量

    Matlab使用heap method管理内存。当在Matlab heap中没有足够的内存使用时,它会向系统请求内存。但是只要内存碎片可以存下当前的变量,Matlab会重新使用内存。

    比如:

a = rand(1e6,1);

b = rand(1e6,1);

  使用大约15.4 MB RAM

c = rand(2.1e6,1);使用近似16.4 MB RAM:

a = rand(1e6,1);
b = rand(1e6,1);
clear
c = rand(2.1e6,1);

    使用32.4 MB RAM。因为Matlab不能使用a、b被clear的空间,因为它们均小于2.1 MB,而同时它们也很可能是不连续的。
    最好的方法:

c = rand(2.1e6,1);
clear
a = rand(1e6,1);
b = rand(1e6,1);

    使用16.4 MB RAM


2.尽量避免产生大的瞬时变量,当它们不用的时候应该及时clear。
3.尽量的重复使用变量
(跟不用的clear掉一个意思)
4.将矩阵转化成稀疏形式
    如果矩阵中有大量的0,最好存储成稀疏形式。稀疏形式的矩阵使用内存更少,执行时间更短。
例如:1000×1000的矩阵X,它2/3的元素为0,使用两种存储方法的比较:

Name          X                   Y

Size      1000x1000           1000x1000

Bytes       8000000             4004000

Class    double array      double array (sparse)

5.使用pack命令

    当内存被分为很多碎片以后,其实本身可能有很大的空间,只是没有作构的连续空间即大的Block而已。如果此时Out of Memory,此时使用pack命令可以很好的解决此问题。
6.如果可行的话,将一个大的矩阵划分为几个小的矩阵,这样每一次使用的内存减少。
7.增大内存

 

问题三: Increase the size of the swap file.

    wap space的设置与使用的操作系统有关,具体的如下:
1. UNIX
    Information about swap space can be procured by typing pstat -s at the UNIX command prompt. For detailed information on changing swap space, ask your system administrator.
2. Linux
    Swap space can be changed by using the mkswap and swapon commands. For more information on the above commands, type man followed by the command name at the Linux prompt.
3. Windows NT
    Follow the steps shown here: Right-click the My Computer icon, and select Properties. Select the Performance tab and click the Change button to change the amount of virtual memory.
4.Windows 2000
    右键“我的电脑”->属性->高级->性能->设置,从而改变其虚拟内存。
5. Windows XP
    右键“我的电脑”->属性->高级->性能->设置,从而改变其虚拟内存。   %% 试了, 不行


问题四:尽量少时用系统资源(对于Windows)
    Windows中字体、窗口等都是要占用系统资源的,所以在Matlab运行时尽量不要打开不用的窗口。


问题五:Reloading Variables on UNIX Systems
    由于我不使用UNIX系统,这里不介绍,直接从Help中粘过来
    On UNIX systems, MATLAB does not return memory to the operating system even after variables have been cleared. This is due to the manner in which UNIX manages memory. UNIX does not accept memory back from a program until the program has terminated. So, the amount of memory used in a MATLAB session is not returned to the operating system until you exit MATLAB.
To free up the memory used in your MATLAB session, save your workspace variables, exit MATLAB, and then load your variables back in.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
function GRABIT Extracts data points from an image file. % % GRABIT starts a GUI program for extracting data from an image file. % It is capable of reading in BMP, JPG, TIF, GIF, and PNG files (anything % that is readable by IMREAD). Multiple data sets can be extracted from a % single image file, and the data is saved as an n-by-2 matrix variable in % the workspace. It can also be renamed and saved as a MAT file. % % Following steps should be taken: % 1. Load the image file. % 2. Calibrate axes dimensions. You will be prompted to select 4 points % on the image. Zoom and pan enabled. % 3. Grab points by clicking on points. Right-click to delete a point. % Image can be zoomed and panned. % 4. Multiple data sets will remain in memory so long as the GUI is open. % Variables can be renamed, saved to file, or edited in Array Editor. % % Panning is achieved by clicking and dragging on the image. Double-click % to center view. Right click and drag to zoom in and out. In addition, % there are keyboard shortcuts for zooming: % - zoom in % - zoom out % - reset view % % This code will also work for extracting data points from a tilted or a % skewed image (even upside-down or mirrored). The calibration stage % ensures that the imperfect orientation or quality of the image is % accounted for. % % The types of files that will most likely work are BMP, JPG, TIF, GIF (up % to 8-bit), and PNG files. Basically, any format supported by the IMREAD % is accepted. % % GRABIT(FILENAME) will start the GUI program and open the image file % FILENAME.
function [idx, C, sumD, D] = kmeans(X, k, varargin) % varargin:实际输入参量 if nargin 1 % 大于1刚至少有一种距离 error(sprintf('Ambiguous ''distance'' parameter value: %s.', distance)); elseif isempty(i) % 如果是空的,则表明没有合适的距离 error(sprintf('Unknown ''distance'' parameter value: %s.', distance)); end % 针对不同的距离,处理不同 distance = distNames{i}; switch distance case 'cityblock' % sort 列元素按升序排列,Xord中存的是元素在原始矩阵中的列中对应的大小位置 [Xsort,Xord] = sort(X,1); case 'cosine' % 余弦 % 计算每一行的和的平方根 Xnorm = sqrt(sum(X.^2, 2)); if any(min(Xnorm) <= eps * max(Xnorm)) error(['Some points have small relative magnitudes, making them ', ... 'effectively zero.\nEither remove those points, or choose a ', ... 'distance other than ''cosine''.'], []); end % 标量化 Xnorm(:,ones(1,p))得到n*p的矩阵 X = X ./ Xnorm(:,ones(1,p)); case 'correlation' % 线性化 X = X - repmat(mean(X,2),1,p); % 计算每一行的和的平方根 Xnorm = sqrt(sum(X.^2, 2)); if any(min(Xnorm) 1 error(sprintf('Ambiguous ''start'' parameter value: %s.', start)); elseif isempty(i) error(sprintf('Unknown ''start'' parameter value: %s.', start)); elseif isempty(k) error('You must specify the number of clusters, K.'); end start = startNames{i}; % strcmp比较两个字符串 uniform是在X中随机选择K个点 if strcmp(start, 'uniform') if strcmp(distance, 'hamming') error('Hamming distance cannot be initialized with uniform random values.'); end % 标量化后的X Xmins = min(X,1); Xmaxs = max(X,1); end elseif isnumeric(start) % 判断输入是否是一个数 这里的start是一个K*P的矩阵,表示初始聚类中心 CC = start; % CC表示初始聚类中心 start = 'numeric'; if isempty(k) k = size(CC,1); elseif k ~= size(CC,1); error('The ''start'' matrix must have K rows.'); elseif size(CC,2) ~= p error('The ''start'' matrix must have the same number of columns as X.'); end if isempty(reps) reps = size(CC,3); elseif reps ~= size(CC,3); error('The third dimension of the ''start'' array must match the ''replicates'' parameter value.'); end % Need to center explicit starting points for 'correlation'. % 线性距离需要指定具体的开始点 % (Re)normalization for 'cosine'/'correlation' is done at each % iteration.每一次迭代进行“余弦和线性”距离正规化 % 判断是否相等 if isequal(distance, 'correlation') % repmat复制矩阵1*P*1 线性化 CC = CC - repmat(mean(CC,2),[1,p,1]); end else error('The ''start'' parameter value must be a string or a numeric matrix or array.'); end % ------------------------------------------------------------------ % 如果一个聚类丢失了所有成员,这个进程将被调用 if ischar(emptyact) emptyactNames = {'error','drop','singleton'}; i = strmatch(lower(emptyact), emptyactNames); if length(i) > 1 error(sprintf('Ambiguous ''emptyaction'' parameter value: %s.', emptyact)); elseif isempty(i) error(sprintf('Unknown ''emptyaction'' parameter value: %s.', emptyact)); end emptyact = emptyactNames{i}; else error('The ''emptyaction'' parameter value must be a string.'); end % ------------------------------------------------------------------ % 控制输出的显示示信息 if ischar(display) % strvcat 垂直连接字符串 i = strmatch(lower(display), strvcat('off','notify','final','iter')); if length(i) > 1 error(sprintf('Ambiguous ''display'' parameter value: %s.', display)); elseif isempty(i) error(sprintf('Unknown ''display'' parameter value: %s.', display)); end display = i-1; else error('The ''display'' parameter value must be a string.'); end % ------------------------------------------------------------------ % 输入参数K的控制 if k == 1 error('The number of clusters must be greater than 1.'); elseif n 2 % 'iter',\t 表示向后空出8个字符 disp(sprintf(' iter\t phase\t num\t sum')); end % ------------------------------------------------------------------ % Begin phase one: batch reassignments 第一队段:分批赋值 converged = false; iter = 0; while true % Compute the distance from every point to each cluster centroid % 计算每一个点到每一个聚类中心的距离,D中存放的是N*K个数值 D(:,changed) = distfun(X, C(changed,:), distance, iter); % Compute the total sum of distances for the current configuration. % Can't do it first time through, there's no configuration yet. % 计算当前配置的总距离,但第一次不能执行,因为还没有配置 if iter > 0 totsumD = sum(D((idx-1)*n + (1:n)')); % Test for a cycle: if objective is not decreased, back out % the last step and move on to the single update phase % 循环测试:如果目标没有减少,退出到最后一步,移动到第二阶段 % prevtotsumD表示前一次的总距离,如果前一次的距离比当前的小,则聚类为上一次的,即不发生变化 if prevtotsumD 2 % 'iter' disp(sprintf(dispfmt,iter,1,length(moved),totsumD)); end if iter >= maxit, % break(2) break; % 跳出while end end % Determine closest cluster for each point and reassign points to clusters % 决定每一个点的最近分簇,重新分配点到各个簇 previdx = idx; % 大小为n*1 % totsumD 被初始化为无穷大,这里表示总距离 prevtotsumD = totsumD; % 返回每一行中最小的元素,d的大小为n*1,nidx为最小元素在行中的位置,其大小为n*1,D为n*p [d, nidx] = min(D, [], 2); if iter == 0 % iter==0,表示第一次迭代 % Every point moved, every cluster will need an update % 每一个点需要移动,每一个簇更新 moved = 1:n; idx = nidx; changed = 1:k; else % Determine which points moved 决定哪一个点移动 % 找到上一次和当前最小元素不同的位置 moved = find(nidx ~= previdx); if length(moved) > 0 % Resolve ties in favor of not moving % 重新分配而不是移动 括号中是一个逻辑运算 确定需要移动点的位置 moved = moved(D((previdx(moved)-1)*n + moved) > d(moved)); end % 如果没有不同的,即当前的是最小元素,跳出循环,得到的元素已经是各行的最小值 if length(moved) == 0 % break(3) break; end idx(moved) = nidx(moved); % Find clusters that gained or lost members 找到获得的或者丢失的成员的分簇 % 得到idx(moved)和previdx(moved)中不重复出现的所有元素,并按升序排列 changed = unique([idx(moved); previdx(moved)])'; end % Calculate the new cluster centroids and counts. 计算新的分簇中心和计数 % C(changed,:)表示新的聚类中心,m(changed)表示聚类标号在idx中出现的次数 % sort 列元素按升序排列,Xsort存放对的元素,Xord中存的是元素在原始矩阵中的列中对应的大小位置 [C(changed,:), m(changed)] = gcentroids(X, idx, changed, distance, Xsort, Xord); iter = iter + 1; % Deal with clusters that have just lost all their members % 处理丢失所有成员的分簇,empties表示丢失元素的聚类标号 不用考虑 empties = changed(m(changed) == 0); if ~isempty(empties) switch emptyact case 'error' % 默认值,一般情况下不会出现 error(sprintf('Empty cluster created at iteration %d.',iter)); case 'drop' % Remove the empty cluster from any further processing % 移走空的聚类 D(:,empties) = NaN; changed = changed(m(changed) > 0); if display > 0 warning(sprintf('Empty cluster created at iteration %d.',iter)); end case 'singleton' if display > 0 warning(sprintf('Empty cluster created at iteration %d.',iter)); end for i = empties % Find the point furthest away from its current cluster. % Take that point out of its cluster and use it to create % a new singleton cluster to replace the empty one. % 找到离聚类中心最远距离的点,把该点从它的聚类中移走,用它来创建一个新的聚类,来代替空的聚类 % 得到列的最大元素(dlarge),以及该元素在列中的标号(lonely) [dlarge, lonely] = max(d); from = idx(lonely); % taking from this cluster 从当前聚类移走 C(i,:) = X(lonely,:); m(i) = 1; idx(lonely) = i; d(lonely) = 0; % Update clusters from which points are taken % 更新那些点被移走的聚类 [C(from,:), m(from)] = gcentroids(X, idx, from, distance, Xsort, Xord); changed = unique([changed from]); end end end end % phase one % ------------------------------------------------------------------ % Initialize some cluster information prior to phase two % 为第二阶段初始化一些先验聚类信息 针对特定的距离,默认的是欧氏距离 switch distance case 'cityblock' Xmid = zeros([k,p,2]); for i = 1:k if m(i) > 0 % Separate out sorted coords for points in i'th cluster, % and save values above and below median, component-wise % 分解出第i个聚类中挑选的点的坐标,保存它的上,下中位数 % reshape把矩阵分解为要求的行列数m*p % sort 列元素按升序排列,Xord中存的是元素在原始矩阵中的列中对应的大小位置 Xsorted = reshape(Xsort(idx(Xord)==i), m(i), p); % floor取比值小或者等于的最近的值 nn = floor(.5*m(i)); if mod(m(i),2) == 0 Xmid(i,:,1:2) = Xsorted([nn, nn+1],:)'; elseif m(i) > 1 Xmid(i,:,1:2) = Xsorted([nn, nn+2],:)'; else Xmid(i,:,1:2) = Xsorted([1, 1],:)'; end end end case 'hamming' Xsum = zeros(k,p); for i = 1:k if m(i) > 0 % Sum coords for points in i'th cluster, component-wise % 对基于分量对第i个聚类的坐标点求和 Xsum(i,:) = sum(X(idx==i,:), 1); end end end % ------------------------------------------------------------------ % Begin phase two: single reassignments 第二阶段:单独赋值 % m中保存的是每一个聚类的个数,元素和为n % find(m' > 0)得到m'中大于0的元素的位置(索引) % 实际情况(默认情况下)changed=1:k changed = find(m' > 0); lastmoved = 0; nummoved = 0; iter1 = iter; while iter < maxit % Calculate distances to each cluster from each point, and the % potential change in total sum of errors for adding or removing % each point from each cluster. Clusters that have not changed % membership need not be updated. % 计算从每一个点到每一个聚类的距离,潜在由于总距离发生变化移除或添加引起的错误。 % 那些成员没有改变的聚类不需要更新。 % % Singleton clusters are a special case for the sum of dists % calculation. Removing their only point is never best, so the % reassignment criterion had better guarantee that a singleton % point will stay in its own cluster. Happily, we get % Del(i,idx(i)) == 0 automatically for them. % 单独聚类在计算距离时是一个特殊情况,仅仅移除点不是最好的方法,因此重新赋值准则能够保证一个 % 单独的点能够留在它的聚类中,可喜的是,自动有Del(i,idx(i)) == 0。 switch distance case 'sqeuclidean' for i = changed % idx存放的距离矩阵D中每一行的最小元素所处的列,也即位置 mbrs = (idx == i); sgn = 1 - 2*mbrs; % -1 for members, 1 for nonmembers % 表示只有一个聚类 if m(i) == 1 % prevent divide-by-zero for singleton mbrs 防止单个成员做0处理 sgn(mbrs) = 0; end Del(:,i) = (m(i) ./ (m(i) + sgn)) .* sum((X - C(repmat(i,n,1),:)).^2, 2); end case 'cityblock' for i = changed if mod(m(i),2) == 0 % this will never catch singleton clusters ldist = Xmid(repmat(i,n,1),:,1) - X; rdist = X - Xmid(repmat(i,n,1),:,2); mbrs = (idx == i); sgn = repmat(1-2*mbrs, 1, p); % -1 for members, 1 for nonmembers Del(:,i) = sum(max(0, max(sgn.*rdist, sgn.*ldist)), 2); else Del(:,i) = sum(abs(X - C(repmat(i,n,1),:)), 2); end end case {'cosine','correlation'} % The points are normalized, centroids are not, so normalize them normC(changed) = sqrt(sum(C(changed,:).^2, 2)); if any(normC 0 % Resolve ties in favor of not moving % 重新分配而不是移动 确定移动的位置 moved = moved(Del((previdx(moved)-1)*n + moved) > minDel(moved)); end if length(moved) == 0 % Count an iteration if phase 2 did nothing at all, or if we're % in the middle of a pass through all the points if (iter - iter1) == 0 | nummoved > 0 iter = iter + 1; if display > 2 % 'iter' disp(sprintf(dispfmt,iter,2,nummoved,totsumD)); end end converged = true; break; end % Pick the next move in cyclic order moved = mod(min(mod(moved - lastmoved - 1, n) + lastmoved), n) + 1; % If we've gone once through all the points, that's an iteration if moved 2 % 'iter' disp(sprintf(dispfmt,iter,2,nummoved,totsumD)); end if iter >= maxit, break; end nummoved = 0; end nummoved = nummoved + 1; lastmoved = moved; oidx = idx(moved); nidx = nidx(moved); totsumD = totsumD + Del(moved,nidx) - Del(moved,oidx); % Update the cluster index vector, and rhe old and new cluster % counts and centroids idx(moved) = nidx; m(nidx) = m(nidx) + 1; m(oidx) = m(oidx) - 1; switch distance case 'sqeuclidean' C(nidx,:) = C(nidx,:) + (X(moved,:) - C(nidx,:)) / m(nidx); C(oidx,:) = C(oidx,:) - (X(moved,:) - C(oidx,:)) / m(oidx); case 'cityblock' for i = [oidx nidx] % Separate out sorted coords for points in each cluster. % New centroid is the coord median, save values above and % below median. All done component-wise. Xsorted = reshape(Xsort(idx(Xord)==i), m(i), p); nn = floor(.5*m(i)); if mod(m(i),2) == 0 C(i,:) = .5 * (Xsorted(nn,:) + Xsorted(nn+1,:)); Xmid(i,:,1:2) = Xsorted([nn, nn+1],:)'; else C(i,:) = Xsorted(nn+1,:); if m(i) > 1 Xmid(i,:,1:2) = Xsorted([nn, nn+2],:)'; else Xmid(i,:,1:2) = Xsorted([1, 1],:)'; end end end case {'cosine','correlation'} C(nidx,:) = C(nidx,:) + (X(moved,:) - C(nidx,:)) / m(nidx); C(oidx,:) = C(oidx,:) - (X(moved,:) - C(oidx,:)) / m(oidx); case 'hamming' % Update summed coords for points in each cluster. New % centroid is the coord median. All done component-wise. Xsum(nidx,:) = Xsum(nidx,:) + X(moved,:); Xsum(oidx,:) = Xsum(oidx,:) - X(moved,:); C(nidx,:) = .5*sign(2*Xsum(nidx,:) - m(nidx)) + .5; C(oidx,:) = .5*sign(2*Xsum(oidx,:) - m(oidx)) + .5; end changed = sort([oidx nidx]); end % phase two % ------------------------------------------------------------------ if (~converged) & (display > 0) warning(sprintf('Failed to converge in %d iterations.', maxit)); end % Calculate cluster-wise sums of distances nonempties = find(m(:)'>0); D(:,nonempties) = distfun(X, C(nonempties,:), distance, iter); d = D((idx-1)*n + (1:n)'); sumD = zeros(k,1); for i = 1:k sumD(i) = sum(d(idx == i)); end if display > 1 % 'final' or 'iter' disp(sprintf('%d iterations, total sum of distances = %g',iter,totsumD)); end % Save the best solution so far if totsumD 3 Dbest = D; end end end % Return the best solution
hmm算法matlab实现和实例 hmm_em.m function [LL, prior, transmat, obsmat, nrIterations] = ... dhmm_em(data, prior, transmat, obsmat, varargin) % LEARN_DHMM Find the ML/MAP parameters of an HMM with discrete outputs using EM. % [ll_trace, prior, transmat, obsmat, iterNr] = learn_dhmm(data, prior0, transmat0, obsmat0, ...) % % Notation: Q(t) = hidden state, Y(t) = observation % % INPUTS: % data{ex} or data(ex,:) if all sequences have the same length % prior(i) % transmat(i,j) % obsmat(i,o) % % Optional parameters may be passed as 'param_name', param_value pairs. % Parameter names are shown below; default values in [] - if none, argument is mandatory. % % 'max_iter' - max number of EM iterations [10] % 'thresh' - convergence threshold [1e-4] % 'verbose' - if 1, print out loglik at every iteration [1] % 'obs_prior_weight' - weight to apply to uniform dirichlet prior on observation matrix [0] % % To clamp some of the parameters, so learning does not change them: % 'adj_prior' - if 0, do not change prior [1] % 'adj_trans' - if 0, do not change transmat [1] % 'adj_obs' - if 0, do not change obsmat [1] % % Modified by Herbert Jaeger so xi are not computed individually % but only their sum (over time) as xi_summed; this is the only way how they are used % and it saves a lot of memory. [max_iter, thresh, verbose, obs_prior_weight, adj_prior, adj_trans, adj_obs] = ... process_options(varargin, 'max_iter', 10, 'thresh', 1e-4, 'verbose', 1, ... 'obs_prior_weight', 0, 'adj_prior', 1, 'adj_trans', 1, 'adj_obs', 1); previous_loglik = -inf; loglik = 0; converged = 0; num_iter = 1; LL = []; if ~iscell(data) data = num2cell(data, 2); % each row gets its own cell end while (num_iter <= max_iter) & ~converged % E step [loglik, exp_num_trans, exp_num_visits1, exp_num_emit] = ... compute_ess_dhmm(prior, transmat, obsmat, data, obs_prior_weight); % M step if adj_prior prior = normalise(exp_num_visits1); end if adj_trans & ~isempty(exp_num_trans) transmat = mk_stochastic(exp_num_trans); end if adj_obs obsmat = mk_stochastic(exp_num_emit); end
### 回答1: "MATLAB out of memory" 的意思是MATLAB程序使用了超过计算机可用内存的空间。这可能是由于处理过大的数据集或者不充分释放内存所导致的。要解决此问题,您可以尝试以下几个步骤: 1. 尝试释放一些内存。在MATLAB中,使用clear命令删除不再需要的变量,或使用pack命令对内存进行整理,以便MATLAB可以更好地管理可用内存。 2. 增加计算机可用内存。可以通过关闭其他占用内存的程序,或者添加更多内存到计算机中来实现。 3. 优化MATLAB代码。您可以使用更高效的算法和数据结构,以减少内存使用。 4. 将数据分批处理。如果您的数据集太大,无法一次处理,可以尝试将数据分成较小的块,并分别处理。 希望这些建议能帮助您解决MATLAB内存不足的问题。 ### 回答2: MATLAB的“out of memory问题通常发生在当计算机上的RAM不足以为正在运行的程序提供足够的内存时。这可能会发生在执行需要大量数据的操作(例如大型矩阵的操作或频繁的I/O操作)时,或者当运行时出现内存泄漏的情况时。 要解决“out of memory问题,可以尝试以下几种解决方案: 1.增加计算机的内存:如果计算机的RAM不足以为程序提供足够的内存,可以考虑增加计算机的内存大小,从而提高能够处理的数据量和程序性能。 2.清除内存:在MATLAB中,可以使用clear命令或者close all命令来释放内存。尽管这种方法不能解决整个内存不足的问题,但是它可以释放一些内存来继续执行程序。 3.使用稀疏矩阵:稀疏矩阵使用更少的内存来存储矩阵数据,并允许更快的计算速度。在操作数据时,可以将大型矩阵转换为稀疏矩阵,从而减少内存使用量。 4.使用前向斜杠:在执行矩阵求逆或解线性方程时,使用前向斜杠(\)运算符比使用逆(inv)函数更有效。这是因为前向斜杠使用更少的内存和计算时间。 5.优化代码:最后,可以通过优化代码来减少内存使用量。优化技术包括使用更少的变量、使用更少的循环、避免重复计算和使用更高效的算法等。 总之,“out of memory问题是一个常见的MATLAB问题,但是有许多方法可以缓解或解决这个问题。这些方法包括增加计算机的内存,清除不必要的内存,使用稀疏矩阵,使用前向斜杠和优化代码。 ### 回答3: MATLAB出现“out of memory”的错误,表示内存被耗尽,无法继续执行程序。解决该问题通常采取以下几种方法: 1. 增加计算机的物理内存:更换更大的硬盘、增加内存条等。 2. 优化程序:对于一些比较复杂、耗费内存的程序,考虑优化算法,减少不必要的内存占用。 3. 减少数据量:对于一些特别大的数据集,可以尝试只保留必需的数据,或者将数据分割成小块进行处理。 4. 使用分布式计算:使用多台计算机进行计算,分解任务,减轻单台计算机的内存压力。 需要注意的是,“out of memory”错误一般不是MATLAB软件本身的问题,而是计算机本身的限制。因此,更有效地解决这个问题需要同时考虑软件和硬件两方面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值