How do I call out a specific column in a matrix?
182 次查看(过去 30 天)
显示 更早的评论
Hi all, I have a code in matlab which reads the data given to it(see attached), and here is my matlab code.
A = csvread('test1.txt');
B = A>1000;
C = A<1000;
Z1 = all(B(:,1:3)|(B(:,1:2)& B(:,4)),2);
Z2 = all(C(:,1:4),2);
X1 = all(B(:,1:3),2);
X2 = any(B(:,1:2),2);
C1 = {'','PowerGrip'};
C2 = {'','PrecisionGrip'};
C3 = {'','No Signal'};
[num2str(A),char(strcat({' '},C1(1+Z1),C2(1+X2),C3(1+Z2)))]
So what I want on Z1, is to set a condition, such that when rows of matrix from column 1 to 3 are greater than 1000, as well as when rows of matrix from columns 1, 2 and 4 are greater than 1000, there will be a display as shown in the outputs below. However, there seems to be some form of error with my Z1 and I cant seem to understand why. Is there a solution to this?
1 个评论
Jan
2018-3-11
there seems to be some form of error with my Z1
Please post the error message or explain the difference between the results and your expectations. It is easier to solve a problem than to guess, what the problem is.
回答(2 个)
Jan
2018-3-11
编辑:Jan
2018-3-11
With some guessing:
Z1 = all(B(:,1:3) | (B(:,1:2) & B(:,4)), 2);
In the part B(:,1:2) & B(:,4) you try to apply the and operator to a [n x 2] matrix and [n x 1] matrix. This cannot work, because the inputs must have the same size (or one can be a scalar). The same happens for the or operation also.
You want to achieve this:
rows of matrix from column 1 to 3 are greater than 1000, as well as when rows of
matrix from columns 1, 2 and 4 are greater than 1000
Isn't this the same as:
All elements of rows 1 to 4 are greater than 1000
? Rows 1:3 are greater than 1000 and rows [1,2,4] are greater than 1000 means: rows 1:4 are greater.
Z1 = all(B(:, 1:4), 2); % Or if B has 4 columns: all(B, 2)
Or maybe you want:
Z1 = all(B(:, 1:2), 2) & (B(:,3) | B(:,4));
CARLOS TOLOSA
2020-10-28
Use indexing and counting. If G is matrix then G(i) is a particular entry of G. 'i' counts vertically so if G=[1 2;3 4] then G(2)=3. So to call a column of G from ith to jth row use G(i:j).
':' counts from i to j.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!