Find the first element in an array
Show older comments
Hey guys, thanks in advance.
I want to find the first element and the last element, besides Nan in the matrix attached how can I do that?
This just gives me Nan
distance_matrix(1);
distance_matrix(2);
7 Comments
Jan
on 16 Jul 2022
It would be useful to know, which dimensions this matrix has. If it is a matrix, it is not clear, what you call "first" and "last", because matrices are 2D. For a vector, it would be:
notNaN = ~isnan(distance_matrix);
first = distance_matrix(find(notNaN, 1, 'first'));
last = distance_matrix(find(notNaN, 1, 'last'));
Miguel Albuquerque
on 16 Jul 2022
distance_matrix is a row vector:
load distance_matrix
whos distance_matrix
so the indices returned from find are indices of columns:
notNaN = ~isnan(distance_matrix);
first_idx = find(notNaN, 1, 'first');
last_idx = find(notNaN, 1, 'last');
disp(first_idx);
disp(last_idx);
Miguel Albuquerque
on 16 Jul 2022
Maybe this (depends on what c is):
load distance_matrix
c = 1; % ?
distance_matrix(distance_matrix==0) = NaN;
[rows,columns]=size(distance_matrix);
[val,idx] = min(distance_matrix);
notNaN = ~isnan(distance_matrix);
first_idx = find(notNaN, 1, 'first');
last_idx = find(notNaN, 1, 'last');
% initialize tshift to NaNs the size of distance_matrix:
tshift = NaN(rows,columns);
% overwrite the elements of tshift where distance_matrix is non-NaN:
tshift(first_idx:last_idx) = ((first_idx:last_idx)-idx).^2./(val.*c);
format compact
disp(tshift); % elements 184 through 276 are non-NaN
Miguel Albuquerque
on 16 Jul 2022
Edited: Miguel Albuquerque
on 16 Jul 2022
That line doesn't belong in a loop. Move it out of the loop like I had it, and it seems to do what you want.
Here it is again, using the variable names you have in the latest version of your code:
load distance_matrix
c = 3e8;
distance_matrix(distance_matrix==0) = NaN;
[rows,columns]=size(distance_matrix);
[R0,ixR0] = min(distance_matrix)
notNaN = ~isnan(distance_matrix);
first_idx = find(notNaN, 1, 'first')
last_idx = find(notNaN, 1, 'last')
% colOfMin is the same as ixR0 (minimum value appears only once)
% so you can use either one below in expression for tshift
[rowOfMin, colOfMin] = find(distance_matrix == R0) % Find row and col of min.
% initialize tshift to NaNs the size of distance_matrix:
tshift = NaN(rows,columns);
% overwrite the elements of tshift where distance_matrix is non-NaN:
tshift(first_idx:last_idx) = ((first_idx:last_idx)-ixR0).^2./(R0.*c);
% first_idx == ixR0, so tshift(first_idx) == 0, as expected:
tshift(first_idx)
Answers (1)
A little simpler:
% Load sample data.
load('distance_matrix.mat')
% Find non-nan indexes.
goodIndexes = find(~isnan(distance_matrix));
% Get the value of distance_matrix at the first non-nan index:
v1 = distance_matrix(goodIndexes(1))
% Get the value of distance_matrix at the last non-nan index:
v2 = distance_matrix(goodIndexes(end))
1 Comment
Miguel Albuquerque
on 16 Jul 2022
Edited: Miguel Albuquerque
on 16 Jul 2022
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!