关于矩阵的几种存储方式 Hayden'Blog

1.矩阵

关于矩阵我们就不解释定义了,这里只说一下矩阵一旦被定义之后,矩阵的行数和列数就不能被改变。

对于一般线性结构的矩阵,我们采用顺序存储结构,以二维数组来存储。

以二维数组存储分为两种主要形式:

  • 以行为序存储
  • 以列为序存储


这样对于一个矩阵,一旦确定了行数和列数,便可以为其分配存储空间,反之,如果给定了矩阵中的第一个元素的存放地址(basic address),我们就可以将矩阵中元素的存储地址表示为其下标的线性函数,这样就可以随机读取或查找矩阵中的任意一个元素。

比如:Loc(a ij)  = Loc(a 11) + [(i-1)*n+(j-1)]*d 我们假设每个元素占用d个单元,aij就是前面所有元素占用的单元数加上基地址。


2.一些特殊的矩阵存储

我们知道如果用一个矩阵中可能会有情况是我们用不上二维数组里面所有的空间,也就是说一个矩阵中我们可能会用到的元素和他占用的空间相差是很大的,这就意味着会造成空间的浪费,因为我们是要为每一个元素开辟存储空间的,比如对于下面的一些特殊矩阵,我们就可以使用特殊的存储方法来存储他们

  • 对称矩阵
    • 对称矩阵就是说一个矩阵中,元素满足a ij = a ji (0<=i,j<=n-1) 
对于对称矩阵,我们就没有必要为每一个元素开辟存储空间了,因为他们有一部分的想同的

我们可以以行为序来存储对称矩阵

我们让第一行放一个元素,第二行放两个元素,第三行放三个元素,以此类推......就是说我们会真正存储的元素个数是 1 + 2 + 3 + …… + n = n*(n+1)/2 等差数列就不多说了

这样我们定义一个SA[n*(n+1)/2] 的数组来存储元素,现在我们要访问 a ij元素 !!!敲黑板了

  • 若 i >= j 说明元素在对称矩阵的下三角部分
                         此时数组中 代表的a ij 的元素之前就 i 行 ,有 1 + 2 + 3 + ...... i = i(i+1)/2 个元素,因此 k(a ij的下标) = i(i+1)/2 + j  

  • 若 i < j  说明元素在对称矩阵的上三角部分
                         因为a ij = a ji ,所以同理以上 ,将i和j互换即可 得到: k = j(j+1)/2 + i;

这样一来:

k = i*(i+1)+j i>=j

k = j*(j+1)+i i<j


以上就是对称矩阵的存储方式,等下有时间再更新压缩存储的,要去上课了 orz .... 


         



  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Matlab 实现 EMD(经验模态分解)的示例代码: ```matlab function [imf,residual] = emd(x) % Empirical Mode Decomposition % x - input signal (must be a column vector) % imf - matrix of intrinsic mode functions (each as a column) % residual - residual signal % reference: Huang et al., "The empirical mode decomposition and the Hilbert spectrum for nonlinear and non-stationary time series analysis," Proc. R. Soc. Lond. A, Vol. 454, pp. 903-995, 1998. % author: Hayden Schaeffer % date: 1/25/2019 % set stopping criterion and maximum number of iterations epsilon = 0.05; numIter = 1000; % initialize residual r = x; % initialize counter nIMF = 0; % initialize matrix of intrinsic mode functions imf = []; while ~ismonotonic(r) && nIMF < numIter % initialize iteration h = r; % extract local maxima and minima maxmin = zeros(length(r),2); maxmin(:,1) = islocalmax(h); maxmin(:,2) = islocalmin(h); % identify number of extrema nExtrema = sum(maxmin(:,1)) + sum(maxmin(:,2)); % limit iterations if nExtrema < 3 break; end % iterate until monotonic while ~ismonotonic(h) % interpolate local maxima and minima pmax = interp1(find(maxmin(:,1)),h(maxmin(:,1)),1:length(h),'pchip','extrap'); pmin = interp1(find(maxmin(:,2)),h(maxmin(:,2)),1:length(h),'pchip','extrap'); % calculate mean envelope m = (pmax + pmin)/2; % calculate difference between signal and mean envelope d = h - m; % update residual r = r - d; % update iteration h = d; % increment iteration counter nIMF = nIMF + 1; % limit iterations if nIMF > numIter break; end end % add current IMF to matrix imf = [imf r]; % update residual r = x - sum(imf,2); % check stopping criterion if sum(abs(r)) < epsilon break; end end % add final residual to matrix residual = r; end function tf = ismonotonic(x) % check if vector is monotonic tf = ~(any(diff(x) > 0) && any(diff(x) < 0)); end ``` 使用示例: ```matlab % generate test signal t = linspace(0,1,1000)'; x = sin(2*pi*50*t) + sin(2*pi*120*t) + sin(2*pi*200*t); % perform EMD [imf,residual] = emd(x); % plot results figure; subplot(length(imf)+1,1,1); plot(t,x); title('Input Signal'); xlabel('Time (s)'); ylabel('Amplitude'); ylim([-3 3]); for k = 1:length(imf) subplot(length(imf)+1,1,k+1); plot(t,imf(:,k)); title(['IMF ' num2str(k)]); xlabel('Time (s)'); ylabel('Amplitude'); ylim([-1 1]); end subplot(length(imf)+1,1,length(imf)+1); plot(t,residual); title('Residual'); xlabel('Time (s)'); ylabel('Amplitude'); ylim([-1 1]); ``` 这将生成一个包含输入信号及其每个 IMFs 和残差的图形。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值