MatLab function for each two values in struct inside loop
显示 更早的评论
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 个评论
Stephen23
2019-3-29
"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
2019-3-29
Luna
2019-3-29
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
2019-3-29
编辑:Mohammed Hammad
2019-3-29
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!