# How to declare above type of variable in MATLAB?

24 views (last 30 days)
Gaurab Raj Neupane on 29 Sep 2020
Answered: Ameer Hamza on 30 Sep 2020 at 11:41
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?

Show 1 older comment
KSSV on 29 Sep 2020
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.
Gaurab Raj Neupane on 30 Sep 2020 at 10:53
How can I declare the above type of variable before hand and fill up the numbers in loop?
Gaurab Raj Neupane on 30 Sep 2020 at 11:09
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]} ?

Stephen Cobeldick on 30 Sep 2020 at 11:02
"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

#### 1 Comment

Gaurab Raj Neupane on 30 Sep 2020 at 11:16
Thank you very much!
धन्यवाद

Ameer Hamza on 30 Sep 2020 at 11:41
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));

### Community Treasure Hunt

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

Start Hunting!

Translated by