How to add a specific number to elements in a vector?
Show older comments
Hi,
I have a 1x600 matrix called 'samples' containing numbers from 0 to 255. I want to add the number 255 to every element in the array that is less than 100. I also want to divide the matrix into two different matrices, one contains the elements 0 to 300 and the other one from 301 to 600. I am able to divide the matrix in 2, but when I add the value 255 to every element less than 100 it adds it to all the elements in the matrix. How can I fix my error? Thanks! The code I have so far is
%samples is a 1x600 matrix
frames = 300;
on = wkeep(samples,frames,[frames+1 1]);
off = wkeep(samples,frames,[1 1]);
if any (on < 100)
on_count = on + 255;
end
if any (off < 100)
off_count = off +255 ;
end
2 Comments
Giacomo Maurelli
on 29 Jan 2018
%samples is a 1x600 matrix
% Loop over elements
for i=length(samples)
% Define if i-element of "samples" is greater of 100 or not
if samples(1,i)<100
% Adding 255 to elements that are less than 100
samples(1,i)=samples(1,i)+255;
end
end
% Definind matrix size
frames = 300;
% Creating the two matrices
matrix1 = samples(1,1:frames);
matrix2 = samples(1,frames+1:length(samples))
Ivonne Escorcia
on 5 Feb 2018
Accepted Answer
More Answers (1)
Giacomo Maurelli
on 29 Jan 2018
Edited: Walter Roberson
on 29 Jan 2018
%samples is a 1x600 matrix
% Loop over elements
for i=length(samples)
% Define if i-element of "samples" is greater of 100 or not
if samples(1,i)<100
% Adding 255 to elements that are less than 100
samples(1,i)=samples(1,i)+255;
end
end
% Definind matrix size
frames = 300;
% Creating the two matrices
matrix1 = samples(1,1:frames);
matrix2 = samples(1,frames+1:length(samples))
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!