简体   繁体   English

分别查看矩阵中的每一行(Matlab)

[英]Look at each row separately in a matrix (Matlab)

I have a matrix in Matlab(2012) with 3 columns and X number of rows, X is defined by the user, so varies each time. 我在Matlab(2012)中有一个矩阵,其中包含3列和X行数,X由用户定义,因此每次都不同。 For this example though I will use a fixed 5x3 matrix. 对于本示例,我将使用固定的5x3矩阵。

So I would like to perform an iterative function on each row within the matrix, while the value in the third column is below a certain value. 因此,我想在矩阵的每一行上执行迭代函数,而第三列中的值低于某个特定值。 Then store the new values within the same matrix, so overwrite the original values. 然后将新值存储在同一矩阵中,因此覆盖原始值。

The code below is a simplified version of the problem. 下面的代码是问题的简化版本。

M=[-2 -5 -3 -2 -4]; %Vector containing random values

Vf_X=M+1; %Defining the first column of the matrix
Vf_Y=M+2; %Defining the secound column of the matrix
Vf_Z=M; %Defining the third column of the matrix

Vf=[Vf_X',Vf_Y',Vf_Z']; %Creating the matrix

while Vf(:,3)<0 
Vf=Vf+1;
end
disp(Vf)

The result I get is 我得到的结果是

 1     2     0
-2    -1    -3
 0     1    -1
 1     2     0
-1     0    -2

Ideally I would like to get this result instead 理想情况下,我想获得此结果

 1     2     0
 1     2     0
 1     2     0
 1     2     0
 1     2     0

The while will not start if any value is above zero to begin with and stops as soon as one value goes above zero. 如果任何值都大于零,则while不会开始,而一旦一个值大于零,它将停止。

I hope this makes sense and I have supplied enough information 我希望这是有道理的,并且我提供了足够的信息

Thank you for your time and help. 感谢您的时间和帮助。

Your current problem is that you stop iterating the very moment any of the values in the third row break the condition. 当前的问题是,在第三行中的任何值破坏条件的那一刻,您就停止了迭代。 Correct me if I'm wrong, but what I think you want is to continue doing iterations on the remaining rows, until the conditions are broken by all third columns. 如果我错了,请纠正我,但是我想您要继续其余行进行迭代,直到所有第三列都打破了条件为止。

You could do that like this: 您可以这样做:

inds = true(size(Vf,1),1);
while any(inds)
    Vf(inds,:) = Vf(inds,:)+1;
    inds = Vf(:,3) < 0;
end

Of course, for the simple addition you provide, there is a better and faster way: 当然,对于您提供的简单添加,有一种更好,更快的方法:

inds = Vf(:,3)<0; 
Vf(inds,:) = bsxfun(@minus, Vf(inds,:), Vf(inds,3));

But for general functions, the while above will do the trick. 但是对于一般功能,上面的while可以解决问题。

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

相关问题 Java 二维数组(矩阵)。 每行的总和分别 - Java 2D array (matrix). Sum of each row separately 如何在Matlab矩阵的每一行中删除多余的重复元素? - How to remove extra duplicated elements in each row of a matrix in matlab? 在Matlab中对3维矩阵的每一行中的元素进行排序 - Sort elements in each row of a 3-d matrix in Matlab 在 Matlab 中构造一个矩阵,跟踪每行中的相等元素 - Construct a matrix in Matlab that keeps track of equal elements in each row 如何分别循环每一行? - How to loop each row separately? 在Matlab中的每第n行之后,在矩阵中的每个序列中插入一行到另一个矩阵 - Inserting One Row Each Time in a Sequence from Matrix into Another Matrix After Every nth Row in Matlab MATLAB:如何生成每行和每列具有特定数量 1 的随机二进制矩阵? - MATLAB : How can I generate a random binary matrix with a specific number of 1s in each row and in each column? 对角化矩阵的每一行 - “Diagonalize” each row of a matrix 如何在MATLAB中将mxn矩阵转换为逗号分隔的文本文件(每行为)? - How to convert m x n matrix to (each row as) comma separted text file in MATLAB? Matlab:添加&#39;convolving&#39;行和colum来制作每个元素组合的方阵? - Matlab: Adding 'convolving' row and colum to make a square matrix of each element combination?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM