Find the first element in an array

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

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'));
Yes sorry that is exactly what I want, and if I want to know the column that first and last correspond in distance_matrix?
distance_matrix is a row vector:
load distance_matrix
whos distance_matrix
Name Size Bytes Class Attributes distance_matrix 1x400 3200 double
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);
184
disp(last_idx);
276
Thanks a lot, that was it,
just one question, I have this code I created just now, I do a calculation tshift, but In this array tshift I only want values between first_idx and last_idx, but I need this tshift to be 1x400, so my idea was to put Nan in the columns that are not filled, in this case all except bewteen first_idx and last_idx. However, sometimes my conditions in the calculations differ and my first_idx could be 1 and last_idx could be 100, in that case tshift would be zero between 100:400, Is there a way I could automatize this:
Thanks a lot once more
[rows,columns]=size(distance_matrix);
distance_matrix(distance_matrix==0) = NaN ;
[val,idx] = min(distance_matrix);
for col = 1 : columns
thisColumn = distance_matrix(:, col);
idx2=col-idx;
tshift(:,col)=(idx2.^2)./ (val.*c);
end
notNaN = ~isnan(distance_matrix);
first_idx = find(notNaN, 1, 'first');
last_idx = find(notNaN, 1, 'last');
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
Columns 1 through 19 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 20 through 38 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 39 through 57 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 58 through 76 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 77 through 95 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 96 through 114 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 115 through 133 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 134 through 152 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 153 through 171 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 172 through 190 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 0.0118 0.0471 0.1059 0.1883 0.2942 0.4237 Columns 191 through 209 0.5767 0.7532 0.9533 1.1769 1.4240 1.6947 1.9889 2.3066 2.6479 3.0128 3.4011 3.8130 4.2485 4.7074 5.1900 5.6960 6.2256 6.7787 7.3554 Columns 210 through 228 7.9556 8.5793 9.2266 9.8974 10.5917 11.3096 12.0511 12.8160 13.6045 14.4165 15.2521 16.1112 16.9939 17.9001 18.8298 19.7830 20.7598 21.7602 22.7840 Columns 229 through 247 23.8314 24.9024 25.9969 27.1149 28.2564 29.4215 30.6102 31.8223 33.0580 34.3173 35.6000 36.9064 38.2362 39.5896 40.9665 42.3670 43.7910 45.2385 46.7096 Columns 248 through 266 48.2042 49.7224 51.2641 52.8293 54.4180 56.0303 57.6662 59.3256 61.0085 62.7149 64.4449 66.1984 67.9755 69.7761 71.6002 73.4479 75.3191 77.2138 79.1321 Columns 267 through 285 81.0739 83.0393 85.0282 87.0406 89.0766 91.1361 93.2191 95.3257 97.4558 99.6095 NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 286 through 304 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 305 through 323 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 324 through 342 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 343 through 361 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 362 through 380 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Columns 381 through 399 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Column 400 NaN
Hey I have done this way, based on your code
c=3e8;
However last line:
tshift(first_idx:last_idx)=((first_idx:last_idx)-idx2).^2./(R0.*c);
This doesn´t calculate well the tshif, I want thar idx2= difference between col and colofMin, basically a distance between the column where I am and the column corresponding to the minimum value of the matrix.
How can I do this calculation but just for between first_idx and last_idx
[val,idx] = min(distance_matrix);
for col = 1 : columns
thisColumn = distance_matrix(:, col);
idx2=col-idx;
tshift(:,col)=(idx2.^2)./ (val.*c);
The entire code:
[rows,columns]=size(distance_matrix);
[R0,ixR0]=min(distance_matrix(distance_matrix ~= 0));
first_idx = find(distance_matrix, 1, 'first');
last_idx = find(distance_matrix, 1, 'last');
[rowOfMin, colOfMin] = find(distance_matrix == R0); % Find row and col of min.
tshift=zeros(rows,columns);
for col = 1 : columns
thisColumn = distance_matrix(:, col);
idx2=col-colOfMin;
tshift(first_idx:last_idx)=((first_idx:last_idx)-idx2).^2./(R0.*c);
end
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)
R0 = 84.9718
ixR0 = 184
notNaN = ~isnan(distance_matrix);
first_idx = find(notNaN, 1, 'first')
first_idx = 184
last_idx = find(notNaN, 1, 'last')
last_idx = 276
% 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.
rowOfMin = 1
colOfMin = 184
% 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)
ans = 0

Sign in to comment.

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))
v1 = 84.9718
% Get the value of distance_matrix at the last non-nan index:
v2 = distance_matrix(goodIndexes(end))
v2 = 111.3272

1 Comment

I created this:
If I want to calculate this just for this columns, how could I do it:
c=3e8;
first_idx=184;
last_idx=276;
calculation just for this= tshift((first_idx:last_idx)
calculatation I want to do= (idx2.^2)./(R0.*c)
[rows,columns]=size(distance_matrix);
[R0,ixR0]=min(distance_matrix(distance_matrix ~= 0));
first_idx = find(distance_matrix, 1, 'first');
last_idx = find(distance_matrix, 1, 'last');
[rowOfMin, colOfMin] = find(distance_matrix == R0); % Find row and col of min.
tshift=zeros(rows,columns);
for col = 1 : columns
thisColumn = distance_matrix(:, col);
idx2=col-colOfMin;
tshift(first_idx:last_idx)=(idx2.^2)./(R0.*c);
end

Sign in to comment.

Products

Tags

Asked:

on 16 Jul 2022

Commented:

on 16 Jul 2022

Community Treasure Hunt

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

Start Hunting!