Clear Filters
Clear Filters

error in applying the condition to the if loop

1 view (last 30 days)
Hi. I need to change all lines with 101 and [100;101] in 'test_2' with '[]' in both 'test_2' and 'test_1'.
I succeeded with 101, but can't get the same result with [100;101].
'test_1_out' and 'test_2_out' are the results I need to obtain.
test_1 = importdata("test_1.mat");
test_2 = importdata("test_2.mat");
for K = 1:length(test_1)
if test_2{K,1} == 101
test_1{K,1} = [];
test_2{K,1} = [];
end
if test_2{K,1} == [100;101] % error
test_1{K,1} = [];
test_2{K,1} = [];
end
end
  1 Comment
Diwakar Diwakar
Diwakar Diwakar on 17 Jul 2023
test_1 = importdata("test_1.mat");
test_2 = importdata("test_2.mat");
for K = 1:length(test_1)
if test_2(K, 1) == 101 || isequal(test_2(K, 1), [100;101])
test_1(K, 1) = [];
test_2(K, 1) = [];
end
end
save('test_1_out.mat', 'test_1');
save('test_2_out.mat', 'test_2');

Sign in to comment.

Accepted Answer

Diya Tulshan
Diya Tulshan on 17 Jul 2023
Hii Alberto Arci,
I understand you want to debug the code and get the desired output.
Solution given below might be one of the possible workflow:-
test_1 = importdata("test_1.mat");
test_2 = importdata("test_2.mat");
for K = 1:length(test_1)
if isequal(test_2{K,1}, 101) || isequal(test_2{K,1}, [100;101])
test_1{K,1} = [];
test_2{K,1} = [];
end
end

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!