Why is my iteration not working?
Show older comments
I have an example of how to find the difference of elements of a matrix starting on the 2nd iteration which would correlate with the 2nd row and j=1 is corresponding to the column. So This example Shows the difference of 2nd row - 1st row on the first column all the way to the last column.
Then next iteration will do 3rd row - 2nd row on the first column all the way to the last column and replace all those difference into the 'u' matrix.
This example is successful with the first column of 'u' being zeros and the rest being all different non-zero values.
a = rand(30,1,51);
u = zeros(30,1,51);
for i=2:numel(u(1,:,:)
for j =1:numel(u(:,:,1))
u(j,:,i) = (a(j,:,i) - a(j,:,i-1));
end
end
squeeze(u)
squeeze(a)
Now This is the problem that is similar to the example but, I cant seem to figure out why my 'u' is all zeros still.
All LatPos positions in the matrix for the first 15 rows and all columns are all different values. So 'u' should be a non-zero scalar at each position
function [LatPos, u] = fcn(detections)
LatPos = zeros(numel(detections.Detections),numel(detections.Detections(1).Time)) %LatPos = zeros[30x51]... Ends up becoming [30x1x51]
%detections.Detections = [30x1]
%detections.Detections(1).Time = [1x1x51]
u = zeros(numel(detections.Detections),numel(detections.Detections(1).Time)) %% u = zeros[30x51]... Ends up becoming [30x1x51]
for i = 2:numel(u(1,:,:))
for j = 1:numel(u(:,:,1)
u(j,:,i) = LatPos(j,:,i) - LatPos(j,:,i-1)
end
end
Could the issue be in the initialization of my matrices for both 'u' or 'LatPos'?
10 Comments
Voss
on 20 Sep 2023
You say:
%LatPos = zeros[30x51]... Ends up becoming [30x1x51]
and "All LatPos positions in the matrix for the first 15 rows and all columns are all different values."
but neither of these claims are seen in the code you've shared with us.
The code we see has LatPos as a matrix of all zeros, then u is calculated as differences of different parts of LatPos (all of which are still all zeros), so of course u is all zeros at the end.
Marshall Botta
on 20 Sep 2023
Edited: Marshall Botta
on 20 Sep 2023
Torsten
on 20 Sep 2023
You don't get an error in this line because of the capital Z in "Zeros" ?
u = Zeros (numel(detections.Detections),numel(detections.Detections(1).Time)
?
You have i-1 in both places on the right side here:
u(j,:,i) = LatPos(j,:,i-1) - LatPos(j,:,i-1);
So that's subtracting something from itself, which will result in zeros (unless the something is NaN or infinity).
The original code had i and i-1. Should it be i and i-1? If so, check/change it and see if that gives you some non-zero values.
Voss
on 20 Sep 2023
There are also a lot of missing close parentheses on lines like this one:
for i = 2:numel(u(1,:,:) % <- missing ")"
I don't know if that's because of an error/artifact of copy-pasting to here or if that's missing in your real code, but in general it's necessary for us to have the actual code you're running, so please check those places too.
Marshall Botta
on 21 Sep 2023
Torsten
on 21 Sep 2023
Why do you initialize LatPos and u as 2d arrays
LatPos = zeros(numel(detections.Detections),numel(detections.Detections(1).Time));
u= zeros(numel(detections.Detections),numel(detections.Detections(1).Time));
and later on use them as 3d arrays:
LatPos(j,:,i-1)
u(j,:,i)
?
We can only speculate what the problem is without getting the data in "detections".
Image Analyst
on 21 Sep 2023
Come on, let's not drag this out over more posts and more days with your copy and pasting mistakes. Simply attach your m-file with the paperclip icon after you read this:
Marshall Botta
on 21 Sep 2023
Marshall Botta
on 21 Sep 2023
Accepted Answer
More Answers (0)
Categories
Find more on Time Series Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
