Workaround for subscript indices being non-integers and non-logicals

2 ビュー (過去 30 日間)
jrb13
jrb13 2017 年 4 月 25 日
コメント済み: jrb13 2017 年 4 月 25 日
Hello,
I'm trying to return a submatrix with all rows that have a matching column value, but I'm receiving this error: Subscript indices must either be real positive integers or logicals.
The error is in line 22 of the attached code. I cannot round my array values to an integer, and I'm not sure if there is a more obvious or efficient way of sorting my data by a column value (column 5), reporting the median of column 2 values (distances) for all rows with matching values in column 1 (data from the same time point).
I am looking for advice on how to write the given function to do what I described above without indexing into a matrix using matrix with nonintegers. I am relatively new to MATLAB and there is likely a more efficient way to execute this, but I'm not sure what question to ask yet.
Please let me know if I can provide more information. Thank you for any help.

採用された回答

Walter Roberson
Walter Roberson 2017 年 4 月 25 日
You do not show us the code for SortTime for the line
sorted_Time = SortTime(RelevantRows);
but we can predict that it is returning the sorted data, rather than the sort index. But later when you use
M2=sorted_Time;
AllTimes = M2(:,1);
then M2 will be the contents of the rows, not the indices. But then you do
TrackData = M2(AllTimes,:);
which assumes that what you are working with in AllTimes is indices, not direct values.
You need to go back and re-think which of your variables are to be indices and which of them are to be the contents of the array.
I suspect you are using the form
B = sortrows(A,column)
but you instead probably want to use the form
[B,index] = sortrows(A,column)
  1 件のコメント
jrb13
jrb13 2017 年 4 月 25 日
Thank you! This really helped me think about what I'm doing. I was using B = sortrows(A,[column,column]). I didn't think about sorted_Time changing from indices to direct values, I thought I was just renaming sorted_Time for ease of writing/reading. I definitely don't think I would have caught/understood that I'm using AllTimes as indices in TrackData = M2(AllTimes,:); so thanks for that.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 4 月 25 日
There's no such thing as the 1.5th element of a vector or matrix. Perhaps you want to perform interpolation instead of or in addition to indexing?

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by