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

215 visualizaciones (últimos 30 días)
jon
jon el 11 de Mzo. de 2018
Respondida: CARLOS TOLOSA el 28 de Oct. de 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 comentario
Jan
Jan el 11 de Mzo. de 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.

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 11 de Mzo. de 2018
Editada: Jan el 11 de Mzo. de 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 comentario
jon
jon el 11 de Mzo. de 2018
Editada: jon el 11 de Mzo. de 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)

Iniciar sesión para comentar.


CARLOS TOLOSA
CARLOS TOLOSA el 28 de Oct. de 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.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by