matlab 矩阵每行找到第一个不是0的位置,把前面的0全部删掉,如果不对齐后面可以补0,这个该怎么写呢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab 矩阵每行找到第一个不是0的位置,把前面的0全部删掉,如果不对齐后面可以补0,这个该怎么写呢相关的知识,希望对你有一定的参考价值。
参考技术A A = randint(8,6,[0 1]) % 生成实验矩阵[m,n] = size(A);
for p = 1:m
tem = A(p,:);
id = 1;
while tem(1)==0 & id<n
tem(1) = [];
tem = [tem 0];
id = id+1;
end
A(p,:) = tem;
end
disp(A)
A =
0 0 1 1 1 0
0 0 0 1 1 0
1 0 0 0 0 1
1 0 0 0 1 0
0 1 1 0 0 1
1 0 0 0 0 1
0 0 0 0 1 0
0 1 1 1 1 0
1 1 1 0 0 0
1 1 0 0 0 0
1 0 0 0 0 1
1 0 0 0 1 0
1 1 0 0 1 0
1 0 0 0 0 1
1 0 0 0 0 0
1 1 1 1 0 0
参考技术B %% A是要处理的矩阵B = zeros(size(A));
for i = 1:size(A, 1)
t = A(i,:);
t = t(find(t, 1, 'first'):end);
B(i,1:length(t)) = t;
end
B本回答被提问者采纳 参考技术C 编写循环,每次处理一位就可以了。
如何制作每行元素总和为1的概率矩阵,并且每行在matlab中具有统一的概率
【中文标题】如何制作每行元素总和为1的概率矩阵,并且每行在matlab中具有统一的概率【英文标题】:How to make probability matrix with each row elements sum 1 and each row have uniform probability in matlab 【发布时间】:2016-10-25 14:49:25 【问题描述】:我找到了matlab文件(https://in.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum),它生成概率均匀的概率矩阵,每列总和为1。文件如下
function [x,v] = randfixedsum(n,m,s,a,b)
%[x,v] = randfixedsum(n,m,s,a,b)
%
% This generates an n by m array x, each of whose m columns
% contains n random values lying in the interval [a,b], but
% subject to the condition that their sum be equal to s. The
% scalar value s must accordingly satisfy n*a <= s <= n*b. The
% distribution of values is uniform in the sense that it has the
% conditional probability distribution of a uniform distribution
% over the whole n-cube, given that the sum of the x's is s.
%
% The scalar v, if requested, returns with the total
% n-1 dimensional volume (content) of the subset satisfying
% this condition. Consequently if v, considered as a function
% of s and divided by sqrt(n), is integrated with respect to s
% from s = a to s = b, the result would necessarily be the
% n-dimensional volume of the whole cube, namely (b-a)^n.
%
% This algorithm does no "rejecting" on the sets of x's it
% obtains. It is designed to generate only those that satisfy all
% the above conditions and to do so with a uniform distribution.
% It accomplishes this by decomposing the space of all possible x
% sets (columns) into n-1 dimensional simplexes. (Line segments,
% triangles, and tetrahedra, are one-, two-, and three-dimensional
% examples of simplexes, respectively.) It makes use of three
% different sets of 'rand' variables, one to locate values
% uniformly within each type of simplex, another to randomly
% select representatives of each different type of simplex in
% proportion to their volume, and a third to perform random
% permutations to provide an even distribution of simplex choices
% among like types. For example, with n equal to 3 and s set at,
% say, 40% of the way from a towards b, there will be 2 different
% types of simplex, in this case triangles, each with its own
% area, and 6 different versions of each from permutations, for
% a total of 12 triangles, and these all fit together to form a
% particular planar non-regular hexagon in 3 dimensions, with v
% returned set equal to the hexagon's area.
%
% Roger Stafford - Jan. 19, 2006
% Check the arguments.
if (m~=round(m))|(n~=round(n))|(m<0)|(n<1)
error('n must be a whole number and m a non-negative integer.')
elseif (s<n*a)|(s>n*b)|(a>=b)
error('Inequalities n*a <= s <= n*b and a < b must hold.')
end
% Rescale to a unit cube: 0 <= x(i) <= 1
s = (s-n*a)/(b-a);
% Construct the transition probability table, t.
% t(i,j) will be utilized only in the region where j <= i + 1.
k = max(min(floor(s),n-1),0); % Must have 0 <= k <= n-1
s = max(min(s,k+1),k); % Must have k <= s <= k+1
s1 = s - [k:-1:k-n+1]; % s1 & s2 will never be negative
s2 = [k+n:-1:k+1] - s;
w = zeros(n,n+1); w(1,2) = realmax; % Scale for full 'double' range
t = zeros(n-1,n);
tiny = 2^(-1074); % The smallest positive matlab 'double' no.
for i = 2:n
tmp1 = w(i-1,2:i+1).*s1(1:i)/i;
tmp2 = w(i-1,1:i).*s2(n-i+1:n)/i;
w(i,2:i+1) = tmp1 + tmp2;
tmp3 = w(i,2:i+1) + tiny; % In case tmp1 & tmp2 are both 0,
tmp4 = (s2(n-i+1:n) > s1(1:i)); % then t is 0 on left & 1 on right
t(i-1,1:i) = (tmp2./tmp3).*tmp4 + (1-tmp1./tmp3).*(~tmp4);
end
% Derive the polytope volume v from the appropriate
% element in the bottom row of w.
v = n^(3/2)*(w(n,k+2)/realmax)*(b-a)^(n-1);
% Now compute the matrix x.
x = zeros(n,m);
if m == 0, return, end % If m is zero, quit with x = []
rt = rand(n-1,m); % For random selection of simplex type
rs = rand(n-1,m); % For random location within a simplex
s = repmat(s,1,m);
j = repmat(k+1,1,m); % For indexing in the t table
sm = zeros(1,m); pr = ones(1,m); % Start with sum zero & product 1
for i = n-1:-1:1 % Work backwards in the t table
e = (rt(n-i,:)<=t(i,j)); % Use rt to choose a transition
sx = rs(n-i,:).^(1/i); % Use rs to compute next simplex coord.
sm = sm + (1-sx).*pr.*s/(i+1); % Update sum
pr = sx.*pr; % Update product
x(n-i,:) = sm + pr.*e; % Calculate x using simplex coords.
s = s - e; j = j - e; % Transition adjustment
end
x(n,:) = sm + pr.*s; % Compute the last x
% Randomly permute the order in the columns of x and rescale.
rp = rand(n,m); % Use rp to carry out a matrix 'randperm'
[ig,p] = sort(rp); % The values placed in ig are ignored
x = (b-a)*x(p+repmat([0:n:n*(m-1)],n,1))+a; % Permute & rescale x
return
但我想生成每行元素总和为 1 且每行在 matlab 中具有统一概率的矩阵。如何使用上述程序执行此操作。运行上面的程序我将它调用到另一个文件并设置参数 即
m=4;n=4; a=0; b=1.5;s=1;
[x,v] = randfixedsum(n,m,s,a,b)
【问题讨论】:
创建第一列并转置 不,列没有统一的概率,所以我不能通过转置来使用 对每一行应用this(用1
而不是100
)?
@JayRam 你是想让整个矩阵的分布均匀还是保持每一行的分布均匀就足够了?
我只希望每一行都是统一的
【参考方案1】:
创建一个随机矩阵并将每一行除以该行元素的总和:
function result = randrowsum(m ,n)
rnd = rand(m,n);
rowsums = sum(rnd,2);
result = bsxfun(@rdivide, rnd, rowsums);
end
创建一个m * n
随机矩阵:
a=randrowsum(3,4)
检查每一行的总和是否为1:
sum(a,2)
【讨论】:
m=3;n=3; a=randrowsum(m,n) 函数结果 = a rnd = rand(m,n); rowsums = sum(rnd,2);结果 = bsxfun(@rdivide, rnd, rowsums) 结束 @JayRam 将函数保存在名为randrowsum.m
的文件中,然后调用函数【参考方案2】:
我想说最简单的是使用给定函数生成数组。
[x,v] = randfixedsum(n,m,s,a,b);
然后只传输结果。
x = x';
【讨论】:
不,列没有统一的概率,所以我不能通过转置来使用以上是关于matlab 矩阵每行找到第一个不是0的位置,把前面的0全部删掉,如果不对齐后面可以补0,这个该怎么写呢的主要内容,如果未能解决你的问题,请参考以下文章
在MATLAB中,如何找出矩阵的非零元素。并且输出它所在的行和列。只要非零就为真。输出1。。