add coloumn to a matrix using if statement

1 view (last 30 days)
Phy
Phy on 10 Jan 2014
Answered: Image Analyst on 10 Jan 2014
I have a matrix A(6x8)[6 rows, 8 columns] ,how can I add a new columns to be A(6x16)if every elements of this matrix satisfy these conditions bellow if A(i,j)<5 add (0), if 5<A(i,j)<10 add (1), if 10<A(i,j)<15 add 2 finally if A(i,j)>15 add 4 I can't reassemble this matrix again when using a loop, so I need now a suitable script to solve my tasks. Please if someone could help me and many thanks to all GAMMAY from EGYPT
  1 Comment
Matt J
Matt J on 10 Jan 2014
Edited: Matt J on 10 Jan 2014
If you're only adding 0,1,2, or 4 columns then the greatest number of additional columns can be 4. Yet, if the final matrix has 16 columns, it means you've added 8.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 10 Jan 2014
Try this:
%a = randi(20, 6,8)
columnsToAdd = 0
if all(a <= 5)
columnsToAdd = 0
elseif all(a > 5 & a <= 10)
columnsToAdd = 2
elseif all(a > 10 & a <= 15)
columnsToAdd = 4
elseif all(a > 15)
columnsToAdd = 6
end
if columnsToAdd > 0
[rows, columns] = size(a)
a = [a, zeros(rows, columnsToAdd )]
end

Matt J
Matt J on 10 Jan 2014
Edited: Matt J on 10 Jan 2014
Hint:
>> A=rand(3,4); newcolumns=2; A(end,end+newcolumns)=0
A =
0.3112 0.6020 0.6892 0.0838 0 0
0.5285 0.2630 0.7482 0.2290 0 0
0.1656 0.6541 0.4505 0.9133 0 0

Categories

Find more on Computational Geometry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!