How can I Remove a Column and add it again?

조회 수: 4 (최근 30일)
bkshn
bkshn 2015년 6월 25일
댓글: Guillaume 2015년 6월 25일
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
Image Analyst
Image Analyst 2015년 6월 25일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2015년 6월 25일
%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
bkshn
bkshn 2015년 6월 25일
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 2015년 6월 25일
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];

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by