row index exceeds table dimensions?
14 次查看(过去 30 天)
显示 更早的评论
I'm getting the error "row index exceeds table dimensions". Not sure how I'm getting this now because I use it earlier in my script and it works fine?
This is the code im using:
SF3 = SF2(MParam,:)
I use similar variations of this code a few times in the earlier lines but all of a sudden it gives me this error and I'm not even surer what it means, nevermind how to fix it.
6 个评论
dpb
2020-4-19
编辑:dpb
2020-4-19
No, I'm not saying that at all...I'm showing you the probable cause of your problem -- we can't know the correct solution lacking any more information on what you're trying to do.
Sorting in your case is almost certainly NOT the correct one...just a machination I used to show the difference one can get depending on data placement between same code with different data.
Reread the explanation and experiment with a sample set of data small enough to examine at command window to fully comprehend the problem.
回答(1 个)
Image Analyst
2020-4-19
This works:
% Create table (because user forgot to attach it).
numRows = 20;
col1 = 2 * rand(numRows, 1);
col2 = 2 * rand(numRows, 1);
SF2 = table(col1, col2)
% Now we have our table and can begin
% Extract column 1 into a column vector M
M = SF2.col1
% Figure out which rows have a value of less than 1.3:
rowsToExtract = M < 1.3
% Extract only those rows into SF3:
SF3 = SF2(rowsToExtract, :)
whos SF3 % Show size of SF3
No error at all. If it doesn't work for you, then attach SF2 in a .mat file
save('answers.mat', 'SF2', 'M');
and attach to your reply.
2 个评论
dpb
2020-4-19
Again I repeat and reiterate -- you are refusing to show us context of how you got your index vector -- IA illustrated that IF you use the index vector on the array from which it was computed the indices will be self-consistent. But in your case you clearly didn't do that or you wouldn't have had the indexing error.
If you just made a typo and wrote
SF3 = SF2(MParam,:)
where SF2 is a typo for some other table name (another reason why using sequentially named variables can lead to coding/logic errors and is recommended against for most cases) that is of the same size as that which was used to calculate MParam, then just fix the typo and go on.
If, otoh, you are trying something more complex than what IA just illustrated, then there's bound to be a way to do it but we need to know what it is specifically that are trying to do. Context is important; we can't see the code that created MParam so we're flying blind and guessing, mostly. (Highly educated guesses, yes, but still based on presumptions, not actual data).
IA's answer doesn't "break up" the table; he just brought the array M out for illustration to make variables that seemed to match up some with your naming (did I mention you haven't shown us how you did that yet?)--it can be done just as well w/o the additional array:
>> % Create table (because user forgot to attach it).
numRows = 20;
col1 = 2 * rand(numRows, 1);
col2 = 2 * rand(numRows, 1);
SF2 = table(col1, col2);
>> SF3=SF2(SF2.col1<1.3,:)
SF3 =
16×2 table
col1 col2
________ ________
1.2441 0.77948
0.7019 0.48338
1.0265 0.80782
0.80362 0.19291
0.15193 0.26395
0.47983 1.8841
0.24664 1.9123
0.36782 1.1504
0.47991 0.11956
0.83453 0.46956
0.099309 0.70632
0.98173 0.086048
0.97851 0.33798
0.67544 1.2982
0.73849 1.2955
0.22241 0.90185
>> whos SF*
Name Size Bytes Class Attributes
SF2 20x2 1592 table
SF3 16x2 1528 table
>>
which does the same operation without any intermediate variables including the explicit logical indexing variable (or indices variable if you used find).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!