Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing specific elements from matrix

Tags:

matlab

I want to remove a (*) asterisk from my matrix and write out that matrix to a text file and the remaining elements will be concatenated to each other without a space or any kind of delimiters. I wrote this code

for b = 1 : length(out7num_r7_nt_back)
    if ~(out7num_r7_nt_back(b) == '*')
        out7num_r7_back(b) = '';
    end
end
disp(out7num_r7_nt_back);
dlmwrite('my_data.txt',out7num_r7_nt_back, '');

I got this error message:

??? Index of element to remove exceeds matrix dimensions.

like image 491
user1818973 Avatar asked Jul 18 '26 16:07

user1818973


1 Answers

You can use a vectorized boolean index, replacing the loop as follows:

out7num_r7_nt_back = out7num_r7_nt_back(out7num_r7_nt_back(b) ~= '*');

That should be much faster as well.

like image 59
Ben Voigt Avatar answered Jul 20 '26 08:07

Ben Voigt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!