Replace array elements with strings
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Jon
am 5 Nov. 2021
Bearbeitet: Jon
am 5 Nov. 2021
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 Kommentare
Jon
am 5 Nov. 2021
Bearbeitet: Jon
am 5 Nov. 2021
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

Weitere Antworten (1)
Steven Lord
am 5 Nov. 2021
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 Kommentare
Siehe auch
Kategorien
Mehr zu Tables 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!