필터 지우기
필터 지우기

How to construct a cell of arrays from an array of arrays?

조회 수: 1 (최근 30일)
hadi
hadi 2015년 4월 12일
댓글: hadi 2015년 4월 13일
Dear all,
I have the following code, and I want to make a cell of arrays from an array of arrays, but I have an error.
The Code:
clc
clear all
close all
for i=1:2
p{i}=[2^i-1 2^(i)];
end
p
Cell={};
for i =1:max(size(p))
Cell(1,i)=p{i};
end
Cell
The error:
??? Conversion to cell from double is not possible.
Error in ==> Untitled5 at 11
Cell(1,i)=p{i};

채택된 답변

Star Strider
Star Strider 2015년 4월 12일
You’re almost there!
Use:
Cell{i}=p{i};
instead, and it works.
  댓글 수: 2
hadi
hadi 2015년 4월 12일
Thank you very much, I appreciate your help.
Star Strider
Star Strider 2015년 4월 12일
My pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 4월 12일
You'd be better off dealing with a regular 2D array. Why mess around with the complications of a cell array when you don't have to.
Even if you did want to have a cell array that was a copy of p, you could just avoid the loop in the first place and just do
Cell = p;
since you're simply setting the ith cell of Cell to the ith cell of p. Though, again, a regular 2D non-cell numerical array will be the simplest by far and you never have to worry about whether to use braces or parentheses or brackets.
By the way, you can use length(p) instead of max(size(p)). And it might not be a good idea to call your cell array Cell. One typo and it becomes cell which is a built-in function.
  댓글 수: 1
hadi
hadi 2015년 4월 13일
Thank you Image Analyst for your advice, I just saw what you were talking about in my second question:
The reason why I am using a cell array is that I have a variable number of raw arrays and with variable sizes, so that the 2D array wont be helpful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by