MatLab function for each two values in struct inside loop
Ältere Kommentare anzeigen
Hello,
I have a struct with some fileds, (attached)
something like exp = {'Names', 'Date', 'X' , 'Y'}
Names = ['John', 'Adam', 'Markus',.....]
X = [ X1, X2, X3, X4,.....]
Y = [ Y1, Y2, Y3, Y4,.....]
I want to run an equation to calculate the sqrt for each two values depending on the same field (Date)
For Date (Date 1)
s12 = sqrt((X2-X1)^2+(Y2-Y1)^2);
then
s13= sqrt((X3-X1)^2+(Y3-Y1)^2);
and
s14= sqrt((X4-X1)^2+(Y4-Y1)^2);
and
s23 = sqrt((X3-X2)^2+(Y3-Y2)^2);
and
s24 = sqrt((X4-X2)^2+(Y4-Y2)^2);
and
s34 = sqrt((X4-X3)^2+(Y4-Y3)^2);
And the same for (Date2)
So I wrote a loop as follows:
for i= 1:length(example)
for j = i+1;
for u = 1:length(unique(example.dates,'rows'));
s(i) = sqrt((example(j).x-example(i).x)^2+(example(j).y-example(i).y)^2);
abr{i} = sprintf('%s-%s',strtrim(example(j).name),strtrim(example(i).name));
end
end
end
%%%%% Export results to an array (Date, Name i_Name j, S)
result = [];
for n = 1:length(unique(example.dates,'rows'));
result = [result; dates(n) abr(n) d(n)];
end
But I am sure there is something wrong because I should get s for all the pairs of the values and just for the same date each time.
The results should be something like that:

Please help me to figure out how to do that.
4 Kommentare
Stephen23
am 29 Mär. 2019
"I have a struct with many fileds,"
"something like struct = {'Names', 'A' , 'B', 'C', ....}"
What you have shown is a cell array, not a structure. You need to learn the difference:
"But I am sure there is something wrong..."
Possibly, but because you have not provided a complete working example, nor any data that matches your description, it is hard for us to know exactly what you are doing or what you want.
It would help if you simply uploaded sample data in one .mat file by clicking the paperclip button, and giving a clear example of the expected output.
Mohammed Hammad
am 29 Mär. 2019
Luna
am 29 Mär. 2019
You have the same names and also same dates, are you sure that your name list will be unique for each date? Otherwise you will get a row like Ronny vs Ronny for example.
Mohammed Hammad
am 29 Mär. 2019
Bearbeitet: Mohammed Hammad
am 29 Mär. 2019
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!