Filter löschen
Filter löschen

How can I Remove a Column and add it again?

2 Ansichten (letzte 30 Tage)
bkshn
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
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
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
bkshn
bkshn am 25 Jun. 2015
How Can I do it Without newimage?
I want to save first column in a variable and add it to my image after doing something.
Guillaume
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];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by