add coloumn to a matrix using if statement
1 view (last 30 days)
Show older comments
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
Answers (2)
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
0 Comments
See Also
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!