How to add a specific number to elements in a vector?

7 ビュー (過去 30 日間)
Ivonne Escorcia
Ivonne Escorcia 2018 年 1 月 29 日
コメント済み: Guillaume 2018 年 2 月 5 日
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 件のコメント
Giacomo Maurelli
Giacomo Maurelli 2018 年 1 月 29 日
%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
Ivonne Escorcia 2018 年 2 月 5 日
Hi, thanks for your answer. I was not able to get the right results with this code. The samples stay the same. But I was able to create the 2 matrices, thanks!

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

採用された回答

Guillaume
Guillaume 2018 年 1 月 29 日
編集済み: Guillaume 2018 年 1 月 29 日
if any (on < 100)
on_count = on + 255;
end
says: if any element of on is less than 100 (1st), then set on_count to the sum of on (hence all the elements of on) with 255. So, if any element of on is less than 100, you add 255 to all the element of on and put it in a new on_count variable. If none of the elements of on are less than 100, then nothing happens, on_count is not even created.
The correct way:
on_count = on; %copy everyting
mask = on_count < 100; %logical vector telling you which elements to change
on_count(mask) = on_count(mask) + 100; %only change those elements for which the mask is true
Same for off. No if or for needed.
edit: another way to obtain the same result with no comparison at all, just using mathematical operations would be:
on_count = mod(on - 100, 255) + 100;
  3 件のコメント
Ivonne Escorcia
Ivonne Escorcia 2018 年 2 月 5 日
Thank you for your answer and for explaining why the if statements do not work in this case. I was able to implement your first suggestion. Can I ask how the second suggestion works? "on_count = mod(on - 100, 255) + 100;"
Guillaume
Guillaume 2018 年 2 月 5 日
"Can I ask how the second suggestion works?"
You're basically constraining your integers to the range 100-355 modulo 255. modulo operations are 0-based, so you need to offset your numbers down to 0 before the modulo operation and back to 100 after.

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

その他の回答 (1 件)

Giacomo Maurelli
Giacomo Maurelli 2018 年 1 月 29 日
編集済み: Walter Roberson 2018 年 1 月 29 日
%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))

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by