필터 지우기
필터 지우기

Hybrid Matrix (strings, double elements)

조회 수: 7 (최근 30일)
Giovanni Gardan
Giovanni Gardan 2020년 7월 17일
편집: Steven Lord 2020년 7월 17일
I have a string-column-vector A and I have three set of data (three string-column-vectors B, C, D) associate to every element of A.
If I put these together to form a compact matrix (We call X), I cannot do operations between elements of B, C, D.
For example if I wanna do a difference between B and C, this is the error:
Undefined operator '-' for input arguments of type 'string'.
This is because my matrix is a string matrix. How can I make operations betweens the numeric values of matrix X?
A = ["wine"; "beer"; "water"; "The"];
B = [1 3 0.5 0.3];
C = [9 26 0.7 900];
D = [0 565 0 0.3];
% I create a compact matrix
X = [A B C D];
% Now I want to make a difference between B and C, but taking the element FROM MATRI X
diff = X(:,2) - X(:,3);
%But this is the error:
Undefined operator '-' for input arguments of type 'string'.

채택된 답변

Steven Lord
Steven Lord 2020년 7월 17일
편집: Steven Lord 2020년 7월 17일
Your concatenation operation (X = [A B C D];) converts the double arrays into string arrays. Instead what you probably want is a table array. I'm going to make one slight modification to your data (transposing B, C, and D.) I'm going to show you two different table arrays, one where A contains data and one where A contains "labels" for the rows of your data.
A = ["wine"; "beer"; "water"; "The"];
B = [1 3 0.5 0.3].';
C = [9 26 0.7 900].';
D = [0 565 0 0.3].';
T_data = table(A, B, C, D)
CminusB = T_data.C - T_data.B
T_label = table(B, C, D, 'RowNames', A)
CminusB2 = T_label.C - T_data.B
beerD = T_label("beer", "D")
waterAll = T_label{"water", :}

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 7월 17일
DifF = double(X(:,2)) - double(X(:,3)); % never name a variable diff!!

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by