简体   繁体   English

在 Matlab 中构造一个矩阵,跟踪每行中的相等元素

[英]Construct a matrix in Matlab that keeps track of equal elements in each row

Suppose I have a IxJ matrix A in Matlab which contains some numbers (possibly, including Inf , -Inf .假设我在 Matlab 中有一个IxJ矩阵A ,其中包含一些数字(可能包括Inf-Inf

For example, for I=3 and J=5 , I could have例如,对于I=3J=5 ,我可以有

A=  [0    0      0 Inf -Inf; 
     5    4      0 Inf -Inf;
     Inf -Inf    0 0   0];

I want to construct a matrix B of size IxJ , such that each row i starts from 1 and adds a +1 every time an element of A(i,:) changes.我想构造一个大小为IxJ的矩阵B ,这样每一行i1开始,并且每次A(i,:)的元素发生变化时都添加一个+1 In the example above在上面的例子中

B=  [1 1 1 2 3;  %
     1 2 3 4 5;
     1 2 3 3 3];

Could you advise on how to proceed?你能建议如何进行吗?

That's easy to do with diff and cumsum .使用diffcumsum很容易做到这一点。

If consecutive inf or inf values should count as different如果连续的infinf值应计为不同

B = cumsum([true(size(A,1),1) diff(A,[],2)~=0], 2);

It works as follows:它的工作原理如下:

  • diff(A,[],2) takes consecutive differences along each row; diff(A,[],2)沿每一行取连续的差异;
  • ~=0 converts nonzero values to 1 ; ~=0将非零值转换为1
  • [true(size(A,1),1)...] prepends a column of true values; [true(size(A,1),1)...]在前面添加一列true值;
  • cumsum(..., 2) accumulates the values along each row. cumsum(..., 2)沿每一行累积值。

This treats inf values as different because inf-inf , or diff([inf inf] , gives NaN rather than 0 .这将inf值视为不同,因为inf-infdiff([inf inf]给出NaN而不是0

If consecutive inf or -inf values should respectively count as equal如果连续的inf-inf值应分别计为相等

Just replace diff(...)~=0 by an expression involving only indexing and ~= :只需将diff(...)~=0替换为仅涉及索引和~=的表达式:

B = cumsum([true(size(A,1),1) A(:,1:end-1)~=A(:,2:end)], 2);

This treats inf values as equal because inf==inf gives true , or equivalently inf~=inf gives false , and similarly for -inf .这将inf值视为相等,因为inf==inf给出true ,或者等效地inf~=inf给出false ,对于-inf也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Matlab 数组的每一行中索引相同/不同的元素 - Indexing equal/different elements in each row of a Matlab array 在Matlab中对3维矩阵的每一行中的元素进行排序 - Sort elements in each row of a 3-d matrix in Matlab 如何在Matlab矩阵的每一行中删除多余的重复元素? - How to remove extra duplicated elements in each row of a matrix in matlab? 如何在MATLAB中从3D矩阵的每一行中选择矢量元素? - How to select vector elements from each row of a 3D matrix in MATLAB? 分别查看矩阵中的每一行(Matlab) - Look at each row separately in a matrix (Matlab) 如何将两列单元格数组转换为带点矩阵(单元格数组每一行中的每对元素)MATLAB - how to convert two-columned cell array into matrix with points (each pair of elements from each row of cell array) MATLAB MATLAB有效地在大矩阵中找到包含三个元素中的两个元素的行 - MATLAB Efficiently find the row that contains two of three elements in a large matrix 在Matlab中的每第n行之后,在矩阵中的每个序列中插入一行到另一个矩阵 - Inserting One Row Each Time in a Sequence from Matrix into Another Matrix After Every nth Row in Matlab 给定每一行对应的索引,从矩阵中获取对应的元素 - Given the indexes corresponding to each row, get the corresponding elements from a matrix 稀疏矩阵每一行中非零元素的平均值 - The mean value of non-zero elements in each row of a sparse matrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM