Finding the first time a number appears in a matrix
3 ビュー (過去 30 日間)
古いコメントを表示
I have matrix c and I am looking to find the first time the number 0.7 appears in the matrix searching row by row.
I have this code to search for it in the first row "columnindex=find(c(1,:)>=0.7,1,'first');" but if it does not appear in the first row how do I search the row below and so on?
Thank you.
0 件のコメント
回答 (1 件)
Star Strider
2021 年 4 月 26 日
Try this —
c = randi([650 750], 50)*1E-3; % Create Matrix
[val,idx] = min(abs(c(:)-0.7)) % Minimum Of Absolute Difference (Linear Index
[crr,ccc] = ind2sub(size(c),idx) % Convert To Subscripts
Check = c(crr,ccc) % Check Result (Delete Later)
.
2 件のコメント
Star Strider
2021 年 4 月 26 日
My pleasure!
The way llinear indexing works, it should scan the first column, then the second column, and so forth.
Transposing ‘c’ first will likely create the linear index appropriately with respect to the ‘ct(:)’ vector to give the result you want, then reversing ‘crr’ and ‘ccc’ in the ind2sub output —
c = rand(7); % Create Matrix
c(3,5) = 0.7 % Specific Element Substitution
ct = c.'; % Transpose
[val,idx] = min(abs(ct(:)-0.7)) % Return Linear Index
[ccc,crr] = ind2sub(size(c),idx)
Check = c(crr,ccc)
.
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!