Find height value from the same horizontal position data

2 Ansichten (letzte 30 Tage)
Irena Hariyanto
Irena Hariyanto am 26 Mai 2021
Beantwortet: Ayush am 4 Jul. 2024
Hi there,
Let's say here I have x-y-z data (location and height) A=[1 1 1; 1 1 2; 1 2 3; 1 2 4; 2 1 1; 2 1 5; 2 2 7]. Also I have x-y data (location only) B=[1 1; 1 4; 1 9; 2 1; 2 4; 2 8]. Then I want to get the z data from the same x-y value, so it should be like C=[1; 2; 1; 5].
Would you help me to solve this? Should I use if or for statement? Thanks a lot.

Antworten (1)

Ayush
Ayush am 4 Jul. 2024
Hi,
To extract the z values from one array (A) based on the matching x-y values provided in another array (B), you can iterate through each row in "B" and use logical indexing to find matching rows in "A". For each match, you can extract the corresponding z values and store them in a cell array. With each iteration, you can concatenate the extracted z values in the cell array to get the final result. Refer to the below implementation for a better understanding:
% Define matrices A and B
A = [1 1 1; 1 1 2; 1 2 3; 1 2 4; 2 1 1; 2 1 5; 2 2 7];
B = [1 1; 1 4; 1 9; 2 1; 2 4; 2 8];
% Initialize a cell array to store the z values
C_cell = cell(size(B, 1), 1);
% Loop through each element in B
for i = 1:size(B, 1)
% Get the current x-y values from B
x_y = B(i, :);
% Find the rows in A that match the current x-y values
matching_rows = A(:, 1) == x_y(1) & A(:, 2) == x_y(2);
% If there are matching rows, extract the corresponding z values
if any(matching_rows)
C_cell{i} = A(matching_rows, 3);
end
end
% Concatenate the cell array into a single column vector
C = vertcat(C_cell{:});
% Display the result
disp(C);
1 2 1 5

Community Treasure Hunt

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

Start Hunting!

Translated by