How can I Remove a Column and add it again?
2 次查看(过去 30 天)
显示 更早的评论
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
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
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 个评论
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!