Replace array elements with strings
조회 수: 19 (최근 30일)
이전 댓글 표시
Hello,
I constructed an array out of three column vectors: Col1, Col2, v.
These Vectors are always equal in length, but the overall length can vary. Col1 and Col2 contain positive intergers 1 to 36, v is a double -1 to 1. The complete array displays corrolations between Polynomials, e.g: 1 (Col1 Index 1) and 3 (Col2 Index 1) corrolate with a value of -0.5 (v Index 1).
Since the Integers 1 to 36 identify the "Type" of a Zernike Polynomial, i want to replace them with the actual name of the Polynomial.
1 is called "Piston"
2 is called "Tilt X"
and so on .
In my array, or in the vectors, how can i change an integer element to a string, depending on it's value?
Thanks in advance!
댓글 수: 0
채택된 답변
Jon
2021년 11월 5일
편집: Jon
2021년 11월 5일
In the example code below I show how to do it with 4 names, but you can expand it to however many names you have.
names = {'Piston','Tilt X', 'someName', 'yetAnotherName'}
Col1 = [1;3;4;2;1;4;3]
Col2 = [2;3;1;4;2;2;3]
v = [-0.9; 0.8; 0.2; -0.8;0.1;0.3;-0.2]
% put columns into a matrix
M = [Col1 Col2 v]
% substitute names based on indices
MwithNames = {names(Col1)' names(Col2)' v}
>> MwithNames{:,1}
ans =
7×1 cell array
{'Piston' }
{'someName' }
{'yetAnotherName'}
{'Tilt X' }
{'Piston' }
{'yetAnotherName'}
{'someName' }
>> MwithNames{:,2}
ans =
7×1 cell array
{'Tilt X' }
{'someName' }
{'Piston' }
{'yetAnotherName'}
{'Tilt X' }
{'Tilt X' }
{'someName' }
>> MwithNames{:,3}
ans =
-0.9000
0.8000
0.2000
-0.8000
0.1000
0.3000
-0.2000
댓글 수: 3
Jon
2021년 11월 5일
편집: Jon
2021년 11월 5일
I think the data is acually in the array you call Output but it is a little hard to see. You can drill down in the workspace tab in your MATLAB IDE, or by looking at them individually, e.g typing Output{:,1} on the command line.
In any case as @Steven Lord suggested, it is probably nicer to put the mixed data (character and numeric) in a table as Ishow in my second example above
추가 답변 (1개)
Steven Lord
2021년 11월 5일
I recommend using a table array to store your data of mixed types.
load patients
T = table(LastName, Age, Smoker);
head(T) % Display just the first few rows of T
LastName is a cell array containing char vectors. Age is a double array. Smoker is a logical array.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!