フィルターのクリア

some basics on MATLAB

2 ビュー (過去 30 日間)
rajesh kumar
rajesh kumar 2023 年 6 月 27 日
編集済み: Torsten 2023 年 6 月 28 日
Q)In a row there are digits like this. how to find the positions greater than zero values in a row is there any instruction please suggest
10 0 25 22 45 0 0 0 0 0 0 1 3 21 after finding i want particular positions to be replace with new integer values only under any
predicted positions(greater than zero values) and remaning all i need to keep 0 value for example
0 0 0 60 0 0 0 0 0 0 0 20 0 0.

採用された回答

Sivsankar
Sivsankar 2023 年 6 月 27 日
編集済み: Torsten 2023 年 6 月 28 日
From what I understood from your question I think the following code solves your question
% Define the row of digits
row = [10, 0, 25, 22, 45, 0, 0, 0, 0, 0, 0, 1, 3, 21];
% Find the positions greater than zero values
positions = find(row > 0);
% Create an array of new integer values
new_values = [60, 20]; % Example new integer values
% Replace the values at the identified positions with new integer values
row(positions) = new_values(1:length(positions));
Index exceeds the number of array elements. Index must not exceed 2.
% Keep the remaining positions as zero
row(row == 0) = 0;
% Display the updated row
disp(row)
Note:This code assumes that the number of positions greater than zero values is the same as the number of new integer values provided. If the number of positions is greater, the extra positions will remain as zero. If the number of positions is fewer, the remaining new values will not be used.
  1 件のコメント
rajesh kumar
rajesh kumar 2023 年 6 月 28 日
Dear JS, Thankyou for ur response, after executing the suggested code i am getting the error "Index exceeds the number of array elements (2).". In the new_values variable only two integers are there i need to insert this two values in any of the positions. The length of the position is 7. Among this 7 positions i need to insert only two values in any of the positions like 2 and 3 or 4 and 5 or 1 and 6 positions.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by