Wrong index acquired or wrong value taken from the index

5 次查看(过去 30 天)
Hello, I am currently doing some data analysis and need to get certain data points with the use of the "max" and "min" functions. That seems to be fine on its own, however I am going through a for loop and have to gather that single point in each case in the loop and store it in a matrix. That again, works fine. However, half of the numbers acquired seem to be wrong. E.g. I have xmx (x_maximum), from which I acquire the index to find the y value ymn at that point. According to all the graphs in ALL x_maximum cases ymn should be positive. The opposite applied to ymn. However for some reason ymx matrix has some negative values and ymn has some positive values.
I have tested each of those cases individually outside of the for loop and that gives the correct answers (positive for ymx and negative for ymn). So something may be wrong in the code itself, but I can't seem to find it.
If there is anyone who could help I would really appreciate it.
Kind regards, Edvardas
Code given below:
%%Displacement and Velocity Front 1 Hz
load('Front_Damper_1Hz'); %B(n)R(m)_F1Hz (notation)
ymx_big=[];
xmx_big=[];
ymn_big=[];
xmn_big=[];
for n=1:11;
figure(1)
Fbump1=cat(3, -B0R0_F1Hz, -B2R0_F1Hz, -B4R0_F1Hz, -B6R0_F1Hz, -B8R0_F1Hz, -B10R0_F1Hz, -B12R0_F1Hz, -B14R0_F1Hz, -B16R0_F1Hz, -B18R0_F1Hz, -B20R0_F1Hz);
Frebound1= cat(3, -B20R0_F1Hz,-B20R2_F1Hz, -B20R4_F1Hz, -B20R6_F1Hz, -B20R8_F1Hz, -B20R10_F1Hz, -B20R12_F1Hz, -B20R14_F1Hz, -B20R16_F1Hz, -B20R18_F1Hz, -B20R20_F1Hz);
subplot(2,2,1) %bump displacement
hold all
plot(Fbump1(:,2,n), Fbump1(:,1,n)); % displacement
% set(gca,'Ydir','reverse')
% set(gca,'Xdir','reverse')
subplot(2,2,2) %rebound displacement
hold all
plot(Frebound1(:,2,n), Frebound1(:,1,n)); % displacement
% set(gca,'Ydir','reverse')
% set(gca,'Xdir','reverse')
subplot(2,2,3) %bump velociy
hold all
plot(Fbump1(:,3,n), Fbump1(:,1,n)); % displacement
% set(gca,'Ydir','reverse')
% set(gca,'Xdir','reverse')
[xmx,loc] = max(Fbump1(:,3,n));
ymx = Fbump1(loc);
xmx_big(n)=xmx;
ymx_big(n)=ymx;
[xmn,loc1] = min(Fbump1(:,3,n));
ymn = Fbump1(loc1);
xmn_big(n)=xmn;
ymn_big(n)=ymn;
subplot(2,2,4) %rebound velocity
hold all
plot(Frebound1(:,3,n), Frebound1(:,1,n)); % displacement
% set(gca,'Ydir','reverse')
% set(gca,'Xdir','reverse')
end
  2 个评论
dpb
dpb 2015-3-11
Nothing much we can do w/o the data that reproduces the problem.
Have you stepped through with the debugger to see where you code logic fails?
Edvardas
Edvardas 2015-3-11
Yes, I have identified the points in the for loop at which the ymx and ymn values seem to be chosen incorrectly (even though the xmx and xmn values are correct).
It's at n = 5; 6; 8; 9; 10
I am attaching the data for this loop in case someone wished to try it out. I've tried using the individual data outside of the "cat" 3d matrix, and in that case managed to get the correct values for ymx and ymn.

请先登录,再进行评论。

采纳的回答

Roger Stafford
Roger Stafford 2015-3-11
In the line
[xmx,loc] = max(Fbump1(:,3,n));
the index 'loc' refers to the row number of the maximum value in the third column of the n-th "page" of Fbump1. However in the next line
ymx = Fbump1(loc);
this same 'loc' index is being used as a linear index and an entirely different element of Fbump is being referenced. I suspect you wanted to reference a different column of the same row and same n-th page. If that column is 4, for example, you should write:
ymx = Fbump1(loc,4,n);
I have the same objection to the two lines:
[xmn,loc1] = min(Fbump1(:,3,n));
ymn = Fbump1(loc1);
  1 个评论
Edvardas
Edvardas 2015-3-11
Yes, that indeed was the problem. Thank you for pointing out, seems to be doing as expected now!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by