Indices of 2D array

5 ビュー (過去 30 日間)
Mohammad Torabi
Mohammad Torabi 2020 年 11 月 4 日
編集済み: Mohammad Torabi 2020 年 11 月 5 日
I have a 1D (very big one) array like below:
data =[ 2 5 0 15 12 7 4 8 19 14 20 25 18 13 40 ];
I want to make the lowest value to zero in every 3 element. I firstly reshaped it to be like:
reshaped_data =
2 15 4 14 18
5 12 8 20 13
0 7 19 25 40
And then sorted each column and now I have the indices of the sorted data as below.
sorted_data_index =
2 1 3 3 3
1 2 2 2 1
3 3 1 1 2
So, please let me know how can I use sorted_data_index as indices of reshaped_data to be able to make the lowest values zero?
I can do this within a for loop, but it would be very slow.
Thanks

採用された回答

Sindar
Sindar 2020 年 11 月 5 日
data =[ 2 5 0 15 12 7 4 8 19 14 20 25 18 13 40 ];
reshaped_data = reshape(data,3,[]);
[~,idx]=sort(reshaped_data,1,'descend');
reshaped_data( sub2ind( size(reshaped_data), idx(3,:), 1:size(reshaped_data,2)) ) = 0
reshaped_data =
2 15 0 0 18
5 12 8 20 0
0 0 19 25 40
  1 件のコメント
Mohammad Torabi
Mohammad Torabi 2020 年 11 月 5 日
編集済み: Mohammad Torabi 2020 年 11 月 5 日
Thanks for your help

サインインしてコメントする。

その他の回答 (1 件)

Luna
Luna 2020 年 11 月 5 日
try this:
data =[ 2 5 0 15 12 7 4 8 19 14 20 25 18 13 40 ];
reshaped_data = reshape(data,3,length(data)/3);
[min_vals,idx] = min(reshaped_data);
idx_increment = 0:3:length(data)-3;
element_number_in_matrix = idx + idx_increment;
reshaped_data(element_number_in_matrix) = 0;
new_data = reshape(reshaped_data,1,length(data));
  1 件のコメント
Mohammad Torabi
Mohammad Torabi 2020 年 11 月 5 日
編集済み: Mohammad Torabi 2020 年 11 月 5 日
Thanks for your help

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by