MATLAB data indexing issue. What is going on here? - arrays
Let I be the identity, D an orthonormal projection, and p a vector.
I realized that several of my lines of code combined to be (I-(I-D))(p) and I could just simplify it to D(p). In replacing it, I computed the new method along-side the old to double check I was computing the same thing (Earlier in my code I had a line that was D = I - D. The D you see here is that D.) I wasn't getting the same answer, and traced it to an error in indexing D.
Here you can see I'm using the debugger and checking portions of D and getting the wrong data returned.
The values in the data explorer on the right are what I'd expect them to be. Sometimes I get what I'd expect from D(:,:,k,1), and sometimes I don't, even when I make the queries right after each other.
The vectors those red arrows are pointing to should be the same. Nothing else changed or was computed between those lines, and k = 2 when the first line was run. I've closed MATLAB and restarted it and get the same issue every time. (D depends on random input, but I'm not altering the seed, so I get the same thing every first run after newly opening MATLAB. The way D is computed, I do expect D(:,:,1,1) to be the identity matrix.)
What in the world is going on? Any help is appreciated.
I have wondered if MATLAB is messing with me on purpose. Sometimes when I open it, a pop-up dialog box says I need to update my student license. I click the update button, but nothing ever happens and the dialog box never closes, so I click cancel.
Edit:
K>> whos D P
Name Size Bytes Class Attributes
D 4-D 4608 double
P 4x1x6 192 double
K>> size(D)
ans =
4 4 6 6
I've been playing around with A and B a bit, and I get the same thing. Sometimes it computes correctly and sometimes it doesn't.
K>> B=permute(P,[1,3,2])
B =
0.4155 0.27554 0.52338 0.6991 -0.11346 0.20999
0.53573 -0.83781 0.53182 -0.022364 0.60291 -0.62601
-0.49246 -0.46111 -0.39168 0.45919 0.42377 0.47074
0.54574 0.097595 0.53835 -0.54763 0.66637 0.58516
K>> A=D
A(:,:,1,1) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,2,1) =
0.99071 -0.091198 0.0020814 -0.029755
-0.091198 0.10503 0.020426 -0.292
0.0020814 0.020426 0.99953 0.0066643
-0.029755 -0.292 0.0066643 0.90473
A(:,:,3,1) =
0.46769 0.019281 -0.49725 0.036486
0.019281 0.9993 0.018011 -0.0013215
-0.49725 0.018011 0.53551 0.034083
0.036486 -0.0013215 0.034083 0.9975
A(:,:,4,1) =
0.96774 0.063488 -0.10826 0.12438
0.063488 0.87506 0.21304 -0.24477
-0.10826 0.21304 0.63673 0.41737
0.12438 -0.24477 0.41737 0.52047
A(:,:,5,1) =
0.7542 0.031217 0.42575 0.056052
0.031217 0.99604 -0.054071 -0.0071187
0.42575 -0.054071 0.26255 -0.097088
0.056052 -0.0071187 -0.097088 0.98722
A(:,:,6,1) =
0.9818 -0.10286 0.085279 0.0034902
-0.10286 0.41855 0.48208 0.01973
0.085279 0.48208 0.60031 -0.016358
0.0034902 0.01973 -0.016358 0.99933
A(:,:,1,2) =
0.99071 -0.091198 0.0020814 -0.029755
-0.091198 0.10503 0.020426 -0.292
0.0020814 0.020426 0.99953 0.0066643
-0.029755 -0.292 0.0066643 0.90473
A(:,:,2,2) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,3,2) =
0.97125 -0.15889 -0.0080537 -0.051131
-0.15889 0.12194 -0.044507 -0.28256
-0.0080537 -0.044507 0.99774 -0.014323
-0.051131 -0.28256 -0.014323 0.90907
A(:,:,4,2) =
0.91488 -0.16388 -0.18495 0.12967
-0.16388 0.6845 -0.35607 0.24964
-0.18495 -0.35607 0.59815 0.28174
0.12967 0.24964 0.28174 0.80247
A(:,:,5,2) =
0.95461 0.16812 0.10326 0.066372
0.16812 0.37733 -0.38244 -0.24582
0.10326 -0.38244 0.76511 -0.15098
0.066372 -0.24582 -0.15098 0.90295
A(:,:,6,2) =
0.99628 0.012018 0.052874 0.027665
0.012018 0.96117 -0.17085 -0.089393
0.052874 -0.17085 0.24833 -0.39329
0.027665 -0.089393 -0.39329 0.79422
A(:,:,1,3) =
0.46769 0.019281 -0.49725 0.036486
0.019281 0.9993 0.018011 -0.0013215
-0.49725 0.018011 0.53551 0.034083
0.036486 -0.0013215 0.034083 0.9975
A(:,:,2,3) =
0.97125 -0.15889 -0.0080537 -0.051131
-0.15889 0.12194 -0.044507 -0.28256
-0.0080537 -0.044507 0.99774 -0.014323
-0.051131 -0.28256 -0.014323 0.90907
A(:,:,3,3) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,4,3) =
0.98622 0.043449 -0.066709 0.085142
0.043449 0.86297 0.21038 -0.26852
-0.066709 0.21038 0.67698 0.41227
0.085142 -0.26852 0.41227 0.47382
A(:,:,5,3) =
0.62859 0.041458 0.47558 0.074661
0.041458 0.99537 -0.053085 -0.0083339
0.47558 -0.053085 0.39105 -0.0956
0.074661 -0.0083339 -0.0956 0.98499
A(:,:,6,3) =
0.95505 -0.16608 0.12371 0.0067153
-0.16608 0.38639 0.45705 0.02481
0.12371 0.45705 0.65956 -0.01848
0.0067153 0.02481 -0.01848 0.999
A(:,:,1,4) =
0.96774 0.063488 -0.10826 0.12438
0.063488 0.87506 0.21304 -0.24477
-0.10826 0.21304 0.63673 0.41737
0.12438 -0.24477 0.41737 0.52047
A(:,:,2,4) =
0.91488 -0.16388 -0.18495 0.12967
-0.16388 0.6845 -0.35607 0.24964
-0.18495 -0.35607 0.59815 0.28174
0.12967 0.24964 0.28174 0.80247
A(:,:,3,4) =
0.98622 0.043449 -0.066709 0.085142
0.043449 0.86297 0.21038 -0.26852
-0.066709 0.21038 0.67698 0.41227
0.085142 -0.26852 0.41227 0.47382
A(:,:,4,4) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,5,4) =
0.73864 0.20112 -0.011394 0.39048
0.20112 0.84524 0.0087678 -0.30047
-0.011394 0.0087678 0.9995 0.017023
0.39048 -0.30047 0.017023 0.41662
A(:,:,6,4) =
0.87322 -0.15647 0.0029936 0.29363
-0.15647 0.80689 0.0036946 0.36238
0.0029936 0.0036946 0.99993 -0.0069332
0.29363 0.36238 -0.0069332 0.31996
A(:,:,1,5) =
0.7542 0.031217 0.42575 0.056052
0.031217 0.99604 -0.054071 -0.0071187
0.42575 -0.054071 0.26255 -0.097088
0.056052 -0.0071187 -0.097088 0.98722
A(:,:,2,5) =
0.95461 0.16812 0.10326 0.066372
0.16812 0.37733 -0.38244 -0.24582
0.10326 -0.38244 0.76511 -0.15098
0.066372 -0.24582 -0.15098 0.90295
A(:,:,3,5) =
0.62859 0.041458 0.47558 0.074661
0.041458 0.99537 -0.053085 -0.0083339
0.47558 -0.053085 0.39105 -0.0956
0.074661 -0.0083339 -0.0956 0.98499
A(:,:,4,5) =
0.73864 0.20112 -0.011394 0.39048
0.20112 0.84524 0.0087678 -0.30047
-0.011394 0.0087678 0.9995 0.017023
0.39048 -0.30047 0.017023 0.41662
A(:,:,5,5) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
A(:,:,6,5) =
0.93556 0.24481 -0.0093576 0.016177
0.24481 0.069855 0.035553 -0.061461
-0.0093576 0.035553 0.99864 0.0023492
0.016177 -0.061461 0.0023492 0.99594
A(:,:,1,6) =
0.9818 -0.10286 0.085279 0.0034902
-0.10286 0.41855 0.48208 0.01973
0.085279 0.48208 0.60031 -0.016358
0.0034902 0.01973 -0.016358 0.99933
A(:,:,2,6) =
0.99628 0.012018 0.052874 0.027665
0.012018 0.96117 -0.17085 -0.089393
0.052874 -0.17085 0.24833 -0.39329
0.027665 -0.089393 -0.39329 0.79422
A(:,:,3,6) =
0.95505 -0.16608 0.12371 0.0067153
-0.16608 0.38639 0.45705 0.02481
0.12371 0.45705 0.65956 -0.01848
0.0067153 0.02481 -0.01848 0.999
A(:,:,4,6) =
0.87322 -0.15647 0.0029936 0.29363
-0.15647 0.80689 0.0036946 0.36238
0.0029936 0.0036946 0.99993 -0.0069332
0.29363 0.36238 -0.0069332 0.31996
A(:,:,5,6) =
0.93556 0.24481 -0.0093576 0.016177
0.24481 0.069855 0.035553 -0.061461
-0.0093576 0.035553 0.99864 0.0023492
0.016177 -0.061461 0.0023492 0.99594
A(:,:,6,6) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Edit 2:
Added relevant code. I've been pausing the code and getting the errors inside the for loops at the end. (I believe it's also giving errors in S, but I've been focusing on D trying to figure it out.)
mtimesx is from here.
n = 4;
M = 6;
P = Normalize(2*rand(n,1,M)-1);
%differences between p_i and p_j
%sum of p_i and p_j
d = Normalize(repmat(permute(P,[1,3,2]),[1,1,M]) - repmat(P,[1,M,1]));
s = Normalize(repmat(permute(P,[1,3,2]),[1,1,M]) + repmat(P,[1,M,1]));
d(isnan(d)) = 0;
%orthogonal projection onto d(:,i,j), i.e. outer product of differences
%orthogonal projection onto s(:,i,j), i.e. outer product of sums
D = mtimesx(permute(d,[1,4,2,3]), permute(d,[4,1,2,3]));
S = mtimesx(permute(s,[1,4,2,3]), permute(s,[4,1,2,3]));
D2 = D;
S2 = S;
%projection onto the complement of d(:,i,j)
%projection onto the complement of s(:,i,j)
D = repmat(eye(n),[1,1,M,M]) - D;
S = repmat(eye(n),[1,1,M,M]) - S;
%total distance to the nearest subspace
PDist = zeros([1,M]);
PDist2 = PDist;
for j = 1:M
for k = 1:M-1
for l = k:M
if j~=k && j~=l
PDist(j) = PDist(j) + min(norm(P(:,1,j) - mtimes(D(:,:,k,l),P(:,1,j))), norm(P(:,1,j) - mtimes(S(:,:,k,l),P(:,1,j))));
PDist2(j) = PDist2(j) + min(norm(D2(:,:,k,1)*P(:,1,j)),norm(S2(:,:,k,1)*P(:,1,j)));
end
end
end
end
PDist-PDist2
Normalize.m
%Normalize
%Accepts an array (of column vectors) and normalizes the columns
function B = Normalize(A)
B = A./repmat(sqrt(sum(A.*A)),size(A,1),1);
end
The problem is that you indexed the matrices using the constant 1 instead of the variable l (lowercase L), both in the first example and in the code for computing PDist2.
In general it is good to avoid using variable names that look similar to each other and/or similar to numbers.
This can be avoided by using an editor that highlights uses different colors for variables and constants (I don't know if this is possible in MATLAB). In fact, this is how I found the error in your code. As you can see, when indexing D2 for the computation of PDist2 the number 1 is colored red.
Related
Trying to find the mean and variance of a certain section of an array for a given number
I have about 30,000 data points but want to find the mean and variance for a certain angle of attack and chart each into their own array but it just won't seem to work clear all clc load('windflies.mat') angleofattacks = [0 1.008 2.016 3.024 4.032 5.04 6.048 7.056 8.064 9.072 10.08 11.088 12.096 13.104 14.112 15.12 16.128 17.136 18.144 19.152 20.16]; WOPFast; [rows,cols] = size(WOPFast); [r1,c2] = size(angleofattacks); count = 0; xc=1; forcexn = zeros(21,1); total = 0; x=[]; for c = 1:21 for r = 1:rows if WOPFast(r,2) == angleofattacks(1,c) x(r,xc)= WOPFast(r,3); end end count = count + 1; forcexn(count,xc)= mean(x); xc =xc +1; end
Calculate radius r = x^2 + y^2 using array values
I would like to use my calculated y[0] (x-values) and y[1] (y-values) from sol_3 using the solve_ivp to find the radius(r) and plot r(t). How can I calculate r using x and y values from sol_r? I keep getting TypeError: only size-1 arrays can be converted to Python scalars. It seems like I am having issues with data type. Please find my code below: timing = np.array([0,2*math.pi]) #t0 = 0 tf=2pi sol_3 = solve_ivp(fun = motion, t_span=[-math.pi, 2*math.pi], y0 = initial, method='RK23') x = (sol_3.y[0, :]) y = (sol_3.y[1, :]) r = math.sqrt(sol_3.y[1,:]**2+sol_3.y[0,:]**2) The array of the sol_3 looks like this: message: 'The solver successfully reached the end of the integration interval.' nfev: 599 njev: 0 nlu: 0 sol: None status: 0 success: True t: array([-3.14159265, -3.14155739, -3.14120473, -3.13767817, -3.12223578, -3.09595606, -3.0602884 , -3.01493539, -2.96220999, -2.90868487, -2.86527118, -2.82204945, -2.76814683, -2.69499434, -2.61108567, -2.51943819, -2.44000487, -2.36057156, -2.28852194, -2.20054302, -2.10648128, -2.03001656, -1.97695204, -1.92388751, -1.87465566, -1.82156056, -1.76673945, -1.73011416, -1.70585555, -1.68159695, -1.64979671, -1.60847127, -1.55783888, -1.50388809, -1.45063319, -1.40126952, -1.3488944 , -1.27797028, -1.19501887, -1.1039372 , -1.02086808, -0.95113058, -0.88636741, -0.80259334, -0.70932861, -0.61584263, -0.55777683, -0.50936554, -0.46344206, -0.4113168 , -0.35663683, -0.31427459, -0.28317047, -0.25206634, -0.22135512, -0.18102468, -0.13108896, -0.07689042, -0.02318517, 0.02738243, 0.07818198, 0.14701599, 0.22918967, 0.31978884, 0.41431515, 0.48493772, 0.55000025, 0.63412425, 0.72775963, 0.82162403, 0.87984573, 0.92849841, 0.97470164, 1.02711062, 1.0820909 , 1.12462382, 1.15583674, 1.18704967, 1.21796912, 1.25856295, 1.30880593, 1.36330716, 1.41729713, 1.46809564, 1.51919004, 1.58841181, 1.67102044, 1.76206092, 1.85696013, 1.92801041,
Fatal error: Index Out of range? BUT IS IT?
I have a very weird array and I am not sure what I am missing, something very simple I am sure. Here is my code, This code loops a struct with arrays of objects and variables and attempts to find the objects which category = 1, when this is true, it sends the array of objects into another array. Everything works fine, until I try to copy the array into the sorted array, more comments in code. I am trying to sort a tableView when the user clicks a certain category, the 4 possible categories are 1,30,31,32 (don't ask why) var courses = [Course]() var sortedCourses = [Course]() #objc func catOthersButtonObj(_ sender: UIButton){ //<- needs `#objc` var count = 0 let courseCount = courses.count let otherCat = 1 // set count to loop the number of detected arrays // this category looks for the number 1 print("courseCount = " + String(courseCount)) // to debug if courseCount is accurate, it is //let noOfCat = courses.categories.count while (count < courseCount) { print("count = " + String(count)) // to debug if count is increasing every loop, it is let totalNoCat = (courses[count].categories?.count)! var catCount = 0 // while entering a loop of arrays of objects, it searches for the array "categories" and sets totalNoCat = number of categories, this is neccesary because one object can have many categories // catCount is used to loop through the number of detect categories if (totalNoCat == 0) { break } // If no categories is detected, next array while (catCount < totalNoCat) { print("totalNoCat = " + String(totalNoCat)) print("catCount = " + String(catCount)) // debug, check if totalNoCat is detected // debug, catCount if number of categories is detected and added if (courses[count].categories?[catCount] == otherCat) { print("category Detected = " + String((courses[count].categories?[catCount])!)) // see if category is 1, when it is 1, this is triggered, so its working fine let currentNoSortedCourses = sortedCourses.count print("currentNoSortedCourses = " + String(currentNoSortedCourses)) // check the number of sorted array, this is to add array into the next array // if there is 0 number of sorted arrays, then sorted[0] should be course[detectarray category = 0 array] //print(courses[count]) checks if courses[count] has data, it does sortedCourses[currentNoSortedCourses] = courses[count] //where the error is //here it is sortedCourses[0] = courses[7] ( 7 is which the array detected the correct category break } catCount = catCount + 1 print("Category Detected = " + String((courses[count].categories?[catCount - 1])!)) //debug, if category not 1, show what it is } count = count + 1 } } Print results courseCount = 50 count = 0 totalNoCat = 2 catCount = 0 Category Detected = 31 totalNoCat = 2 catCount = 1 Category Detected = 30 count = 1 totalNoCat = 2 catCount = 0 Category Detected = 31 totalNoCat = 2 catCount = 1 Category Detected = 30 count = 2 totalNoCat = 1 catCount = 0 Category Detected = 30 count = 3 totalNoCat = 2 catCount = 0 Category Detected = 32 totalNoCat = 2 catCount = 1 Category Detected = 30 count = 4 totalNoCat = 2 catCount = 0 Category Detected = 32 totalNoCat = 2 catCount = 1 Category Detected = 30 count = 5 totalNoCat = 1 catCount = 0 Category Detected = 32 count = 6 totalNoCat = 2 catCount = 0 Category Detected = 32 totalNoCat = 2 catCount = 1 Category Detected = 30 count = 7 totalNoCat = 1 catCount = 0 category Detected = 1 currentNoSortedCourses = 0
Without going much into the structure of the code and how it can be written in a more "Swifty" way, I can see the problem is that this is not a good way to append to an array in Swift. let currentNoSortedCourses = sortedCourses.count sortedCourses[currentNoSortedCourses] = courses[count] This is accessing an index that is out of bounds and causes a crash. To append to an array in Swift use append method. sortedCourses.append(courses[count]) Whilst I'm here I might as well tell you that a better way (one liner) to solve your problem probably be something like this sortedCourses = courses.filter { $0.categories.contains(1) }
how to create multi line chart with single datatable in vb.net
I am working on vb.net application where i have to create a multi line chart. The table coming from the database using storing procedure is:- TagName On Off Trip P1 0 0 1 P2 0 1 1 P3 0 1 0 Q1 0 1 0 Q2 1 0 1 Q3 2 2 2 W1 4 2 1 W2 2 0 1 W3 1 1 0 W4 0 1 1 W5 2 1 1 And the code in vb.net i used to bind the chart named "chTrend" is:- ds = ObjTags.GetTrendData() If (ds.Tables(0).Rows.Count > 0) Then dt = ds.Tables(0) chTrend.DataSource = dt chTrend.Series(0).XValueMember = "TagName" chTrend.Series(0).YValueMembers = "On" chTrend.Series(1).XValueMember = "TagName" chTrend.Series(1).YValueMembers = "Off" chTrend.Series(1).XValueMember = "TagName" chTrend.Series(1).YValueMembers = "Trip" For i = 0 To 2 chTrend.Series(i).ChartType = SeriesChartType.Line chTrend.Series(i).IsVisibleInLegend = True chTrend.Series(i).IsValueShownAsLabel = True chTrend.Series(i).ToolTip = "Data Point Y Value #VALY{G}" chTrend.Series(i).BorderWidth = 3 Next End If When i run the program a Error is coming as following "Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index" At the position chTrend.Series(0).XValueMember = "TagName" how to solve this?
Assuming that your DataTable only has the four DataColumns {TagName, On. Off. Trip}, then replace everything in your shown code below the line: chTrend.DataSource = dt with this. chTrend.Series.Clear() ' clears any existing series Dim s As Series For columnIndex As Int32 = 1 To dt.Columns.Count - 1 Dim name As String = dt.Columns(columnIndex).ColumnName s = chTrend.Series.Add(name) s.XValueMember = dt.Columns(0).ColumnName s.YValueMembers = name s.ChartType = SeriesChartType.Line s.IsVisibleInLegend = True s.IsValueShownAsLabel = True s.ToolTip = "Data Point Y Value #VALY{G}" s.BorderWidth = 3 Next chTrend.DataBind() ' loads the data from dt to the chart The Chart databinding is a not a binding in the normal sense of binding. You need to tell the chart to copy the data from the source so that it has something to plot. That is what the DataBind method does.
Lua: Search key in table, increment index if not and retry
Situation: table = { this, that, something else, x_coord, y_coord } table.x_coord = { 1,2,3,4,7,8,n} table.y_coord = { 2,4,5,9,n} -- numbers aren't fix table.check_boxes = { [12] = a function, [14] = a function, [15] = a function, [24] = a function, [29] = a function, ....(n) } As you can see, the x/y_coords forming check_boxes. For example: table.x_coord[1]..table.y_coord[1] ~ table.check_boxes[1] I use this to move the cursor in the Terminal between the check_boxes. The problem now is in my cursormovement. Currently I got a function that's searching for the next x/y_coord to the left/right/up/down depending on the given input (arrow-keys). With return/space I call the function behind the checkbox. Now, that could set the Cursor on positions where no check_boxes are given. Actually that's not a big deal, because when input == space/return, an inputhandler calls the function at table.check_boxes[table.x_coorx[x_index]..table.y_coords[y_index]] So if the cursor doesn't point on a function, just nothing happens. But now I want the cursor to be forced to the next check_box. What can I do? My Idea so far: following function either for x or y, depending on input left/right up/down: while true do for k, v in pairs(table.check_boxes) do if(table.x_coord[x_index] .. table.y_coord[y_index] == k then break end end -- break -> okay, coord is at a checkbox x_index = x_index + 1 -- or -1 if table.x_coord[x_index] == nil then x_index = 1 end end The Problem now is that the last if will not allow cases like x_coord = {1,3} because it will set x_index to 1 if 2 is reached. Any tips? Edit: Now I got that one going: function cursorTioNextBoxRight() searc_index = x_index search = true while search do search_index = search_index + 1 if search_index > #table.x_coord then search_index = 1 end for k, v in pairs(table.check_boxes) do if tonumber(table.x_coord[search_index..table.y_coord[y_index] == k then x_index = search_index -- YAAAY search = false break end end end I'ts damn slow.
local x_newIndex = x_index + 1 --[[ or -1 --]] x_index = table.x_coord[x_newIndex] and x_newIndex or x_index x_index becomes x_newIndex when x_newIndex exists in the table otherwise it stays the old x_index
function cursorTioNextBoxRight() searc_index = x_index search = true while search do search_index = search_index + 1 if search_index > #table.x_coord then search_index = 1 end for k, v in pairs(table.check_boxes) do if tonumber(table.x_coord[search_index..table.y_coord[y_index] == k then x_index = search_index -- YAAAY search = false break end end end