How to declare above type of variable in MATLAB?

n = {[1,2] [1,6] [2,3] [2,6] [3,4] [3,6] [4,5] [4,6] [5,6]}
How to declare above type of variable in MATLAB?

4 Comments

This is already a valid MATLAB syntax. What is the issue here?
n is of class type cell. You can them using flower braces, n{1}, n{2} etc....you can convert it to a matrix using cell2mat.
How can I declare the above type of variable before hand and fill up the numbers in loop?
Like for getting the matrix A=[1,2,3]
i could declare A= zeros(1,3) then fill the matrix/vector using for loop like
for ii = 1 :3
A(1,ii)=ii;
end
Similarly, how can i declare the matrix like I have done; A= zeros(1,3) for the final output n = {[1,2] [1,6] [2,3] [2,6] [3,4] [3,6] [4,5] [4,6] [5,6]} ?

Sign in to comment.

 Accepted Answer

"How can I declare the above type of variable before hand and fill up the numbers in loop?"
n = cell(1,9);
for k = 1:9
n{k} = [val1,val2];
end

More Answers (1)

An alternative is to use mat2cell() like this
x = [1 2 3 4 5 6 7 8]; % example values
C = mat2cell(x, 1, 2*ones(1,numel(x)/2));

Products

Community Treasure Hunt

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

Start Hunting!