concatenating arrays and matrix

Hi all I trying to get a simple syntax to add this constant to image values
im=imread(im)
im=rgb2gray(im)
so now that im is now the gray scale image
i am try to add these values to the image
C1=[10,20,50,100]
Can profesional provide an example of how this is done in the simplest form using a for loop?
thank you in advance!!!

7 Comments

  1. What is the result of size(im) ?
  2. When you say you want to "add these values to the image" do you mean you want to concatenate them (as mentioned in your title) and if so, where should that vector be inserted?
im=imread('scr.jpg');
im=rgb2gray(im);
C1=[10,20,50,100];
[r,c]=size(im);
m=zeros(r,c);
I do not know where to go from here!!
thanx for acknowledging me!!!
Adam Danz
Adam Danz on 16 Aug 2019
Edited: Adam Danz on 16 Aug 2019
What is the size of im? Could you tell us the result of this line? And could you address my 2nd question above?
size(im)
it is a 5x5 array sorry!
yes i would like to conatenate the two please!
when you say where the vector should be inserted, i am not sure how to code from the syntax that was sent
please keep it simple as I am new to this and I am trying to get the understanding
If your array is 5 x 5 you cannot concatenate a vector with only 4 elements!
Concatenation involves joining two arrays. Here are two examples.
% Horizontal concatenation
A = [1 2 3;
4 5 6];
B = [11 12 13;
14 15 16];
C = [A,B]
C =
1 2 3 11 12 13
4 5 6 14 15 16
% Vertical concatenation
C = [A; B]
C =
1 2 3
4 5 6
11 12 13
14 15 16
Notice that the size of A and B are equal in the dimensions being concatenated. Here's an example that would FAIL.
X = [1 2 3];
Y = [1 2 3 4];
Z = [X,Y]; % This works
Z2 = [X; Y]; % This doesn't work!
To learn more:
Hi adam, it couldn't be clearer than that!! I understood thanx! I will get on it now

Sign in to comment.

 Accepted Answer

nd = ndims(YourImageArray);
Thresholded_arrays = bsxfun(@le, YourImageArray, reshape([50, 100, 150, 200], [ones(1,nd), 4]) );
Instead of @le you would use @plus
nd = ndims(YourImageArray);
Incremented_arrays = bsxfun(@plus, YourImageArray, reshape(uint8([50, 100, 150, 200]), [ones(1,nd), 4]) );

5 Comments

hi walter is that the only way to do it! i find that challenging to understand hence I am back here
thanx for helping me i really appreciate this! thanx load for acknowledging me
No, it is not the only way to do it, but last time you specifically asked to do it "at the same time", and that code does it all "at the same time".
Easier to understand is to just write a simple loop.
Easier to get right is if you know ahead of time whether you are working with grayscale or RGB images, which is something that we as outside observers do not know.
Matpar
Matpar on 17 Aug 2019
Edited: Matpar on 17 Aug 2019
Hi walter thanx for the info! how do you explain a mango to a child? this is what this is for me! i watch vids and read as much as i can but when i jump in! the questions I have usually gets me stuck and there is no one to ask but you guys!!
sometimes the responses I get here are so complicate it leaves me with more question, this puts me off as I myself is unsure of how things should work. Forgive me I will get there one day!
can you show me a simple for loop with this example please?
as for the mentioning the image description, my apologies!
and thanx in advance for assisting me!
C1 = [10,20,50,100];
numC1 = length(C1);
imrgb = imread(FILENAME);
imgray = rgb2gray(imrgb);
[r, c] = size(imgray);
output = zeros(r, c, numC1, class(imgray)); %probably it will be uint8
for C1idx = 1 : numC1
this_C1 = C1(C1idx);
output(:,:,C1idx) = imgray + this_C1;
end
ok i will try it! can you explain the output outside the forloop and inside the for loop? this what happens!! I understood everthing else and how do i make the code myself! i guess this comes with practice but I am willing to learn.
Walter! I must say you have loads of patience and experience!!! I will like to add how grateful I am for having you explain stuff for me in my mess of confusing concerning Matlab!
I am so thankful walter! thank you I appreciate you budz, really thanx much!

Sign in to comment.

More Answers (0)

Asked:

on 16 Aug 2019

Commented:

on 17 Aug 2019

Community Treasure Hunt

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

Start Hunting!