How can I link data from separate arrays?

Suppose you have an array [4 7 9 4 8 3] which represents a list of parameter values within a database. For example ‘4’ is the thickness of a plate which has an associated array containing location and temperature values (5x2 double) [0.55 0.16; 0.60 0.16; 0.65 0.15; 0.7 0.14; 0.75 0.12]. Each of the thickness values has an associated 5x2 array which is unique.
So the question I have is this: Is there a way I can link the location and temperature data arrays with the thickness values so that I can plot all the arrays which correspond to thickness value of ‘4’ without doing this manually?

5 comentarios

dpb
dpb el 26 de Feb. de 2021
Insufficient information -- we know nothing of where these other data are nor how they're defined.
The answer to the general question is "of course" but it does have the caveat that you have to have some piece of information somewhere to use to be able to create that link--whether it's a file-naming convention, the sequence of the associated data in a cell array, ...
Matthew Weltevreden
Matthew Weltevreden el 26 de Feb. de 2021
“you have to have some piece of information somewhere to use to be able to create that link” could you perhaps give an example or link for this please?
dpb
dpb el 26 de Feb. de 2021
Be more helpful to us to have information on how the corollary data are stored; generated; etc., ... to have some idea on how they are linked/associated.
field1 = 'Data1';
field2 = 'Data2';
field3 = 'Data3';
Thickness1 = 19.0;
Thickness2 = 22.0;
Thickness3 = 32.0;
Temp_data1 = [0,-0.05;0.2,-0.13;0.4,0.25;0.6,0.64;0.8,0.56;1,-0.063];
Temp_data2 = [0,0.36;0.2,-0.15;0.4,-0.1;0.6,0.21;0.8,0.47;1,0.35];
Temp_data3 = [0,0.64;0.2,-0.22;0.4,-0.42;0.6,-0.11;0.8,0.28;1,-0.027];
value1={Thickness1,Temp_data1};
value2={Thickness2,Temp_data2};
value3={Thickness3,Temp_data3};
database = struct(field1,value1,field2,value2,field3,value3);
input_thickness = 20;
I've provided some more information to help with the question, assuming the data is part of a struct i would like to extract all the temp data which is larger than a given input thickness in this case 20 (i.e. Temp_data2 & Temp_data3)

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 26 de Feb. de 2021
Editada: dpb el 26 de Feb. de 2021
Making harder than needs must be --
Thickness=[19.0;22.0;32.0];
Temp_data=cat(3l [0,-0.05;0.2,-0.13;0.4,0.25;0.6,0.64;0.8,0.56;1,-0.063];
[0,0.36;0.2,-0.15;0.4,-0.1;0.6,0.21;0.8,0.47;1,0.35];
[0,0.64;0.2,-0.22;0.4,-0.42;0.6,-0.11;0.8,0.28;1,-0.027]);
input_thickness = 20;
isWanted=(Thickness>input_thickness); % logical vector of locations wanted
data=Temp_data(:,:,isWanted); % return temperature data wanted

5 comentarios

Matthew Weltevreden
Matthew Weltevreden el 26 de Feb. de 2021
very helpful, thank you!
This approach has Thickness and Temp_data in separate variables. If Matthew Weltevreden wants to store the data in a combined variable, one question to ask is which form of "slicing" should be easier.
Thickness=[19.0;22.0;32.0];
Temp_data=cat(3, [0,-0.05;0.2,-0.13;0.4,0.25;0.6,0.64;0.8,0.56;1,-0.063], ...
[0,0.36;0.2,-0.15;0.4,-0.1;0.6,0.21;0.8,0.47;1,0.35], ...
[0,0.64;0.2,-0.22;0.4,-0.42;0.6,-0.11;0.8,0.28;1,-0.027]);
structContainingArrays = struct('Thickness', Thickness, 'Temperature', Temp_data);
arrayContainingStructs = struct('Thickness', Thickness(1), ...
'Temperature', Temp_data(:, :, 1));
arrayContainingStructs(2) = struct('Thickness', Thickness(2), ...
'Temperature', Temp_data(:, :, 2));
arrayContainingStructs(3) = struct('Thickness', Thickness(3), ...
'Temperature', Temp_data(:, :, 3));
whos
Name Size Bytes Class Attributes Temp_data 6x2x3 288 double Thickness 3x1 24 double arrayContainingStructs 1x3 1064 struct structContainingArrays 1x1 648 struct
Yes, I know I probably could have written those last lines more simply.
structContainingArrays makes it easier to "slice" the data looking at all the Thicknesses or all the Temperatures at once. It's more difficult to use that to get both Thickness and Temperature for one of the parameter values.
structContainingArrays.Thickness
ans = 3×1
19 22 32
arrayContainingStructs slices the data differently. Getting the Thickness and Temperature for one of the parameter values is easier while getting all the Thickness values or all the Temperatures is harder.
arrayContainingStructs(2)
ans = struct with fields:
Thickness: 22 Temperature: [6×2 double]
Another way to store this data may be to create a table array.
struct2table(arrayContainingStructs)
ans = 3x2 table
Thickness Temperature _________ ____________ 19 {6×2 double} 22 {6×2 double} 32 {6×2 double}
dpb
dpb el 26 de Feb. de 2021
"Another way to store this data may be to create a table array."
Agreed, and probably what I would do in real life...
Thanks for the advice. I was also wondering what could be done if the Temp_data arrays are not identical i.e. if a 8x2 double was also part of the same database such as below?
[0.2,-0.23;0.25,0.029;0.3,0.3;0.35,0.56;0.4,0.78;0.45,0.93;0.5,1.021;0.55,1.02]
dpb
dpb el 27 de Feb. de 2021
That would then require something like the table with the 2D arrays a cells; you wouldn't be able to use the 3D array "trick" as the planes have to be the same dimension(*) to do that.
(*) It would require augmenting to the maximum size with NaN or somesuch to do that.

Iniciar sesión para comentar.

Más respuestas (1)

Hernia Baby
Hernia Baby el 26 de Feb. de 2021
The code below is one I assumed from your infomation.
Therefore, it might be different from what you want to do.
As the other members said, you need to give us enough input data.
Thickness = [4 7 9 4 8 3];
% create datas by the number of thickness datas
name = ("parts"+char('A'+(0:length(Thickness)-1'))');
LocTemp(:,:,1) = [0.55 0.16; 0.60 0.16; 0.65 0.15; 0.7 0.14; 0.75 0.12];
for i = 1:length(Thickness)-1
LocTemp(:,:,i+1) = LocTemp(:,:,i) + 0.01;
end
% seperate data into location and temperature
Location = squeeze(LocTemp(:,1,:))';
Temperature = squeeze(LocTemp(:,2,:))';
% integration as cell type
for i = 1:length(Thickness)
C(i,:) = {name(i),Thickness(i),Location(i,:),Temperature(i,:)};
end
% legends
leg = {'name' 'thickness' 'location' 'temperature'};
% convert type
S = cell2struct(C, leg, 2);
% output example
S(1)
>> S(1)
name: "partsA"
thickness: 4
location: [0.5500 0.6000 0.6500 0.7000 0.7500]
temperature: [0.1600 0.1600 0.1500 0.1400 0.1200]

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 26 de Feb. de 2021

Comentada:

dpb
el 27 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by