简体   繁体   English

如何将条件放入Matlab的特定列中?

[英]How do I put a condition into a specific column for matlab?

would like to check here on what kind of code I could use to create a specific condition on a particular column for matlab. 想在这里检查我可以使用哪种代码在matlab的特定列上创建特定条件。 So here is the case, I have a text file(see image), 这样的话,我有一个文本文件(见图),

在此处输入图片说明

with four columns of values in it. 其中包含四列值。 And here is the code for matlab as given below 这是matlab的代码,如下所示

fileID = fopen('test1.txt');
A = dlmread('test1.txt')
B = A>1000
fclose(fileID);

So according to this line of code, I have imported the file and placed them in a matrix A. Subsequently I set a condition for matrix B whereby A has to be of a greater value than 1000. Using this, I was able to get returns of '0' and '1's, which is what I wanted. 因此,根据这一行代码,我导入了文件并将其放置在矩阵A中。随后,我为矩阵B设置了一个条件,其中A必须具有大于1000的值。使用此方法,我可以获得返回值我想要的是“ 0”和“ 1”。 Now, I want to create a set of conditions such that if column 1, 2 and 3 in any of the rows equals to 1, I will be able to display/print an output in that row that says 'Powergrip'. 现在,我要创建一组条件,以便如果任何行中的第1、2和3列等于1,我将能够在该行中显示/打印输出“ Powergrip”。 Apart from that condition, should 1,2 equals to 1 in any of the rows there would be a display in that row that says 'precisiongrip'? 除了该条件之外,任何行中的1,2是否应等于1,该行中是否会显示“ precisiongrip”? I do believe this has to do with a series of if else conditions but Im not exactly sure on how to go about writing this conditions. 我确实相信这与一系列其他条件有关,但是我不确定如何编写此条件。 Please pardon my poor coding abilities as I'm really new to this. 请原谅我糟糕的编码能力,因为我真的很陌生。 Thank you! 谢谢!

Firstly, you don't need the fopen() and fclose() functions when using dlmread() , just use it by itself (you only need them when you're using other functions like fread() ). 首先,使用dlmread()时不需要fopen()fclose()函数,只需dlmread()使用dlmread()仅在使用fread()等其他函数时才需要它们)。

You can use the all() function to find rows (or columns) which have 1 in all columns (or rows). 您可以使用all()函数查找在所有列(或行)中均包含1的行(或列)。 To find rows which contains 1 in all columns do this: 要查找所有列中均包含1的行,请执行以下操作:

C = all(B, 2); % The "2" means work across columns.

To find cases where there's a 1 in columns 1, 2 and 3, just pass in those three columns: 要查找第1、2和3列中有1的情况,只需传递这三列:

C = all(B(:,[1,2,3]), 2); % PowerGrip

or for columns 1,2: 或对于第1,2列:

D = all(B(:,[1,2]), 2);   % PrecisionGrip

To make an array of descriptions, you need a cell array of strings. 要创建描述数组,您需要一个字符串单元格数组。 There are lots of way you could generate this, here's a simple example: 您可以通过多种方式生成此代码,这是一个简单的示例:

E = cell( size(C) ); % Make a cell array to hold strings, same size as C
E(:) = {'none'};     % Fill all rows with "none" to start. Could use repmat() to create E...
E(C) = {'PowerGrip'}; 
E( D & not(C) ) = {'PrecisionGrip'};

Displaying the words PowerGrip and PrecisionGrip besides the numbers is probably most easily done with a table (though you could do it a number of ways). 使用数字显示单词PowerGrip和PrecisionGrip可能最容易通过table完成(尽管您可以通过多种方法来完成)。 Eg: 例如:

T = array2table(A)
T.Desc = E         % Add a column of descriptions...

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

相关问题 如何使用MATLAB获取一列数据并将其放入自定义大小(mxn)矩阵中? - How do I take a column of data and put it into a custom size (mxn) matrix using MATLAB? 我如何从列B中选择特定数量的行,具体取决于Matlab中列A是否大于0? - How do I select a specific number of rows from column B depending on if Column A is greater than 0 in matlab? 如何将矩阵的值输入到我已经在MATLAB中生成的矩阵的特定列中 - How do i input values of matrix into a specific column in a matrix i have already generated in MATLAB 如何在MATLAB中将变量值放入文本字​​符串? - How do I put variable values into a text string in MATLAB? 如何将迭代结果放入Matlab中的数组 - how do I put the results from an iteration into an array in matlab 如何在Matlab函数中将文件夹作为输入? - How do I put a folder as an input in a matlab function? 我如何在matlab中的if语句中编写1至7天的条件? - how do i write 1 to 7 days condition in if statement in matlab? 如何在Matlab中将数据文本具体转换为整数? - how do i do a specific conversion for a text of data to integers in matlab? MATLAB - 如何创建具有特定要求的条件 - MATLAB - how to create condition with specific requirements 我在哪里在MATLAB中放一个公式 - where do i put a formula in matlab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM