MATLAB Answers

0

Comparing elements in each row of a matrix

Jakub Nosek さんによって質問されました 2017 年 10 月 24 日
最新アクティビティ Bikash Sah さんによって 回答されました 2018 年 7 月 31 日
Hi, I have a matrix AQ_z_matrix where the elements are aircraft altitudes (each column is an aircraft and rows are times at which the altitudes were taken) and I need to compare aircraft's altitudes with each other and find out if the difference (range) is equal or higher than 10000. Then I want to define another matrix called msg_matrix. If the range condition in AQ_z_matrix is met a value 1 is put in the msg_matrix to same row and column as the row and column of the aircraft which are being compared.
To give an example: If AQ_z_matrix=[10000 11000 20000] then msg_matrix would be [1 0 1] as the altitude difference condition is met only between 1st and 3rd aircraft. If AQ_z_matrix was [10000 11000 21000 ] then msg_matrix would be [1 1 2]. Here the condition is met between 1st and 3rd and between 2nd and 3rd aircraft. That means that 3rd aircraft complies the condition twice (it has the altitude difference equal or higher than 10000 in relation with 2 aircraft) and thus there's 2 (1+1) in msg_matrix, for 2nd and 1st aircraft there's only 1 as they only meet the condition in relation with aircraft 1 not between each other.
I have been trying to solve it with this piece of code but it obviously does not compare all values in a row and I don't know how to make it work.
[a, b]=size(AQ_z_matrix);
for i=1:a
for j=1:b
if abs(AQ_z_matrix(i,j))-abs(AQ_z_matrix(i,j+1))>=10000
msg_matrix(i,j)==1;
end
end
end

  2 件のコメント

Jan
2017 年 10 月 24 日
The description is not clear. At first you explain, when to write a 1 to the output, then a 2 appears there. You want to compare all elements with each other, but you get 3 elements as output only. In your code you do not compare to 2, but to 10000*ft2m and write a 10 to the output.
Please edit the question and explain in clear words, what you want to achieve.
Jakub Nosek 2017 年 10 月 24 日
I changed the question. Hope it's clearer now.

サインイン to comment.

2 件の回答

回答者: Cedric Wannaz
2017 年 10 月 24 日
編集済み: Cedric Wannaz
2017 年 10 月 24 日
 採用された回答

Here is one way to do it without any loop (with MATLAB R2016b or more recent):
>> AQ_z_matrix=[10000 11000 20000; 10000 11000 21000]
AQ_z_matrix =
10000 11000 20000
10000 11000 21000
>> msg_matrix = sum(abs(AQ_z_matrix - permute(AQ_z_matrix, [1,3,2])) >= 10000, 3)
msg_matrix =
1 0 1
1 1 2
and here is the same for older versions:
>> msg_matrix = sum(abs(bsxfun(@minus, AQ_z_matrix, permute(AQ_z_matrix, [1,3,2]))) >= 10000, 3)
msg_matrix =
1 0 1
1 1 2
EDIT: note that you were almost there with your loops:
[a, b] = size(AQ_z_matrix) ;
msg_matrix = zeros(a, b) ; % Prealloc(!)
for i = 1 : a
for j = 1 : b
msg_matrix(i,j) = sum(abs(AQ_z_matrix(i,j)-AQ_z_matrix(i,:)) >= 10000) ;
end
end

  2 件のコメント

Jakub Nosek 2017 年 10 月 24 日
Thanks a lot! This is what I was looking for.
Cedric Wannaz
2017 年 10 月 24 日
My pleasure!

サインイン to comment.


回答者: Bikash Sah 2018 年 7 月 31 日

Can someone provide one by one explanation to the above three codes? @Cedric

  0 件のコメント

サインイン to comment.



Translated by