필터 지우기
필터 지우기

Find the first element in an array

조회 수: 357 (최근 30일)
Miguel Albuquerque
Miguel Albuquerque 2022년 7월 16일
댓글: Voss 2022년 7월 16일
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
Miguel Albuquerque
Miguel Albuquerque 2022년 7월 16일
편집: Miguel Albuquerque 2022년 7월 16일
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
Voss
Voss 2022년 7월 16일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2022년 7월 16일
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
Miguel Albuquerque
Miguel Albuquerque 2022년 7월 16일
편집: Miguel Albuquerque 2022년 7월 16일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by