How can I Remove a Column and add it again?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
bkshn
am 25 Jun. 2015
Kommentiert: Guillaume
am 25 Jun. 2015
Hello
I want to remove first column from an Image and do some thing on Image and then add that column again. I want to remove a colum if the number of column is odd. _
The speed and Performance of solution is important for me._
Could you help me?
thanks
1 Kommentar
Image Analyst
am 25 Jun. 2015
We need an explanation of the grammar in "remove a column if the number of column is odd."
Should it say "remove any column if that column number is odd." In other words, do you want to remove columns 1,3,5,7... and keep columns 2,4,6,8,...? If so, do you want the extracted columns in a new matrix, or you want to alter (make narrower) the original matrix?
Or do you mean "if the number of columns in the image is odd" in which case you'd strip off column 1 (and only column 1) if and only if the total number of columns in the image is an odd number?
Those are two different things. Your grammar makes it ambiguous so I need clarification.
Akzeptierte Antwort
Guillaume
am 25 Jun. 2015
%copy original image (img) without first column if number of column is odd
newimage = img(:, 2-mod(size(img, 2), 2):end, :);
%do something with newimage
%??
%add back first column if size is odd.
newimage = [img(:, 1:mod(size(img, 2), 2), :) newimage];
3 Kommentare
Guillaume
am 25 Jun. 2015
This is really basic matrix indexing.
Column 1 of a 2D or 3D matrix is:
img(:, 1, :)
The rest of the image without column one is:
img(:, 2:end, :)
You can copy these into whichever variable you want, eg.:
col1 = img(:, 1, :);
allbutcol1 = img(:, 2:end, :);
and recombine them with the concatenation operators [] (or horzcat):
img2 = [col1, allbutcol1];
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!