Why is my iteration not working?

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

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.
I apologize i forgot to add a line of code, it was minimized in the matlab function.
function [LatPos, u] fcn(detections)
LatPos = zeros(numel(detections.Detections),numel(detections.Detections(1).Time))
u = Zeros (numel(detections.Detections),numel(detections.Detections(1).Time)
LatPosSel = [0,1,0,0,0,0];
%%LatPos for all detections
for i = 1:numel(detections.Detections(1).Time)
LatPos(:,:,i) = LatPosSel * detections.Detections(i).Measurement; %% [0,1,0,0,0,0] * [6x1]
for j = 1:numel(detections.Detections)
LatPos(j,:,:) = LatPosSel * detections.Detections(j).Measurement;
end
end
%%Calculate difference between LatPos starting at time sample 2 = 0.01 1 = 0.00
for i = 2:numel(u(1,:,:)
for j = 1:numel(u(:,:,1)
u(j,:,i) = LatPos(j,:,i-1) - LatPos(j,:,i-1);
end
end
Here is a picture of the output of LatPos
That is just a part of the output, i didnt want to put 3 full pictures of all the values. But this should be enough to understand what the data look like
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)
?
Voss
Voss on 20 Sep 2023
Edited: Voss on 20 Sep 2023

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.

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.

function [LatPos, u] = fcn(detections)
LatPos = zeros(numel(detections.Detections),numel(detections.Detections(1).Time));
u= zeros(numel(detections.Detections),numel(detections.Detections(1).Time));
LatPosSel = [0,1,0,0,0,0];
for i = 1:numel(detections.Detections(1).Time) %% For every time step in the time span of the simulation... N
LatPos(:,:,i) = LatPosSel * detections.Detections(i).Measurement; %% For i time step put i detection into i row in zero matrix
for j = 1:numel(detections.Detections) %% For every detection detected... M
LatPos(j,:,:) = LatPosSel * detections.Detections(j).Measurement; %% For j detection put j detection into j column of the zero matrix
end
end
for i = 2:numel(u(1,:,:))
for j = 1:numel(u(:,:,1))
u(j,:,i) = LatPos(j,:,i) - LatPos(j,:,i-1);
end
end
Here is the updated code. I had trouble copying and pasting the coding straight from the function.
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".
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:
detections.Detections = to a [30x1] matrix
detections.Detections(1).Time = to a [1x1x51] matrix
detections is coming from the a Detection Concatenation block...
The data coming from this block is multi struct/double timseries within a huge data set. I grab them with detections.Detections(i).Measurement which is the pose for ith detection.
The reason I do a 3d array because for some reason putting numel(detections.Detections) for N and numel(detections.Detections(1).Time) u and LatPos matrices become a 3d array using the numel(detections.Detections(1).Time) = 1x1x51 matrix
Here is the whole simulink model

Sign in to comment.

 Accepted Answer

Binaya
Binaya on 26 Sep 2023
Hi Marshal,
I understand that you want to calculate difference of adjacent rows of a given matrix and store it in a separate matrix. For this task, you can use a single loop instead of two for loops the difference by using MATLAB indexing which will simplify the code and increase its readability.
Please find the simplified code below:
for i =2:numel(u(:,:,1))
u(i,:,:) = LatPos(i,:,:) - LatPos(i-1,:,:)
end
The code submitted by you had columns and row indices interchanged, which is fixed in the above code and also simplified so as not to use multiple for loops to access each individual element of the matrix.
I hope this helps.
Regards
Binaya

1 Comment

Thank you Binaya,
This is exactly the solution to my problem. I can't believe it was a lot simpliar than I thought.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!