How to delete a column with a specific value

In my case i would like to delete specific columns where for example the minimum value is 2e11;
This code below doesn't work, i get this error: Matrix index is out of range for deletion.
Error in reactionforce_script (line 16) reactionforce(reactionforce(2:end,:) < thresholdmin | reactionforce(2:end,:) > thresholdmax, :) = []
code :
load ('Y:\Spoormodel\06\reactionforce2.mat');
%reactionforce2 = removevars(reactionforce, 3:2:end);
reactionforce2 = reactionforce;
reactionforce(4:2:end,:) = [];
reactionforce(:,3:2:end) = [];
%reactionforce(:,35:end) = []; %verwijderen van kolommen zonder waarde
reactionforce(1,:) = [];
reactionforce = table2array(reactionforce);
% thresholdmin = 2e11;
% thresholdmax = 2e30;
% reactionforce(reactionforce(2:end,:) < thresholdmin | reactionforce(2:end,:) > thresholdmax, :) = []
%
reactionforce = array2table(reactionforce);
time = table2array(reactionforce(2:end,1));
data = reactionforce(2:end,2:end);
data = table2array(data(1:end,1:end));
plot (time,data);

3 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 5 日
Provide sample data
Jesse
Jesse 2018 年 11 月 5 日
it is a 401x1032 table, these are some values
madhan ravi
madhan ravi 2018 年 11 月 5 日
attach your .mat file

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

 採用された回答

Guillaume
Guillaume 2018 年 11 月 5 日

1 投票

Error in reactionforce_script (line 16)
reactionforce(reactionforce(2:end,:) < thresholdmin | reactionforce(2:end,:) > thresholdmax, :) = []
Well, yes that line is never going to work. If that line is meant to delete all rows for which any value in any columns but the first is outside the boundary, then:
reactionforce(any(reactionforce(2:end,:) < thresholdmin | reactionforce(2:end,:) > thresholdmax), :) = [];

6 件のコメント

Jesse
Jesse 2018 年 11 月 5 日
編集済み: Jesse 2018 年 11 月 5 日
still the same error,
Matrix index is out of range for deletion.
Error in reactionforce_script (line 16) reactionforce(any(reactionforce(2:end,:) < thresholdmin | reactionforce(2:end,:) > thresholdmax), :) = []
Guillaume
Guillaume 2018 年 11 月 6 日
Forgot to tell any to operate across the columns:
reactionforce(any(reactionforce(2:end,:) < thresholdmin | reactionforce(2:end,:) > thresholdmax, 2), :) = [];
However, I've just noticed to you convert your table to a matrix just to perform the deletion and then convert back to a table. That's a complete waste of time. You need to learn to manipulate tables directly.
To operate directly on the table, it's nearly the same syntax except you use {} to access the content of the table:
reactionforce(any(reactionforce{2:end,:} < thresholdmin | reactionforce{2:end,:} > thresholdmax, 2), :) = [];
Jesse
Jesse 2018 年 11 月 6 日
Thanks! It starts to look like something... But, i would like to delete a entire column, if the minimum AND maximum value is between a range. This doens't do that. How can i achieve that?
I changed it to this
reactionforce(any(reactionforce{:,2:end} < thresholdmin | reactionforce{:,2:end} > thresholdmax, 2), :) = [];
Guillaume
Guillaume 2018 年 11 月 7 日
I was a bit puzzled by your question as you asked about deleting columns but your original code deleted rows. So it's definitively columns you want to delete?
I'm not sure what " if the minimum AND maximum value is between a range" means. Do you mean you want to delete a column (which are called variables in a table) if all values are within the range? In which case:
reactionforce(all(reactionforce{2:end, :} >= thresholdmin & rectionforece{2:end, :} <= thresholdmax, 1), :) = [];
Note that since you're now looking at all values along the rows for a column, you use all(xxx, 1) instead of all(xxx, 2) (or any(xxx, 1) instead of any(xxx, 2))
Jesse
Jesse 2018 年 11 月 7 日
Thanks for your help and excellent explanation, this is solving my problem.
madhan ravi
madhan ravi 2018 年 11 月 7 日
+1 perfecto @Guillaume

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

その他の回答 (1 件)

Jesse
Jesse 2018 年 11 月 5 日

0 投票

This is my file

2 件のコメント

Guillaume
Guillaume 2018 年 11 月 5 日
Can you write that as a comment to the question, not an answer?
Jesse
Jesse 2018 年 11 月 5 日
Next time i will do

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2018a

質問済み:

2018 年 11 月 5 日

コメント済み:

2018 年 11 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by