# Create matrix of z values from unique x and y in data structure with unsorted x's and y's

1 回表示 (過去 30 日間)
Thomas Dixon 2020 年 11 月 22 日
I have a data structure with more then 2 columns in it. The first 2 columns are sweep values which when combined make a unique pair. For example I could sweep paramater a from 1:3 and parameter b from -100:10:-80 then measure something/s at each place and create a data structure of the sweep parameters and then the values of anything measured which for now can be just 1 thing (rng for this example).
Typically the data in the first and second column is unsorted which I would like to sort and then mesh with the meshgrid function, then I would like to go through all of my data set and create a 'z matrix' which is the measured values but put into the correct place to match the sorted and meshed [sx,sy] meshgrid.
For example:
a=[2,3,1]
b=[-80,-90,-100]
temp=[7,-63,297;-16,23,0;-9,84,987]
n=1;
for i=1:length(a)
for j=1:length(b)
DataStruct(n).a=a(i)
DataStruct(n).b=b(j)
DataStruct(n).temp=temp(i,j)
n=n+1;
end
end
%%%WHEN a=2 and b=-80 the temperature was 7
%%%WHEN a=1 and b=-90 the temperature was 84
%%%CREATE A SORTED MESH GRID OF a AND b FROM THE DATA STRUCTURE SO I CAN PLOT THEM
%%%CREATE A MATRIX OF THE CORRESPONDIG temp VALUES WHERE THE INDICES HAVE CHANGED SUCH THAT THEY CORRESPOND CORRECTLY WITH THE SORTED a AND b MESHGRID
%%%I THINK THE SORTED MESHGRID WILL LOOK LIKE:
%[sx,sy]=meshgrid(sort(a),sort(b))
%%%I AM HOWEVER DRAWING A BLANK AS TO HOW TO PROGRAMATICALLY CREATE THE CORRECT Z MATRIX SUCH THAT IT IS NOW:
%sz=[987,84,-9;297,-63,7;0,23,-16]
I realise that I may not have formulated this question properly and there is a more precise way of defining the question if this is also obvious I am happy to reword my question such that any solutions are easier to find.
Also obviously I have larger data arrays and multiple instances of this and I don't preallocate my datastructure like i do here (it is read in from experimental data) but the crux is (the x-y pair is unique), (There is one 'z' value per data structure field for an unique x-y pair), (there are potentially lots of unique x-y pairs and so if this is possible without looping over each unique x-y pair and then finding the corresponding z that would be great).
Tom

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

### Community Treasure Hunt

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

Start Hunting!