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!