How do I call out a specific column in a matrix?

182 Ansichten (letzte 30 Tage)
jon
jon am 11 Mär. 2018
Beantwortet: CARLOS TOLOSA am 28 Okt. 2020
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 Kommentar
Jan
Jan am 11 Mär. 2018
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 11 Mär. 2018
Bearbeitet: Jan am 11 Mär. 2018
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));
  1 Kommentar
jon
jon am 11 Mär. 2018
Bearbeitet: jon am 11 Mär. 2018
Sorry for not mentioning, the cases I want are for Columns 1,2 and 3 > 1000, as well as Columns 1,2 and 4.. not for all columns 1 to 4..but I'm not sure how to make a case for 1,2 and 4?
EDIT: I have tried some of your suggestion and have tried using this line
Z1 = all(B(:,1:3),2 & (B(:,1:2)| B(:,4)));
and I got this
Error in Filereadtest (line 4)

Melden Sie sich an, um zu kommentieren.


CARLOS TOLOSA
CARLOS TOLOSA am 28 Okt. 2020
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.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by