Raise elements of cell arrays in the power of 2

조회 수: 1 (최근 30일)
gsourop
gsourop 2016년 11월 18일
댓글: Jay shankar Nanda 2021년 3월 29일
Hi everyone,
I have a cell array A of 2x100, where each element is a scalar and I want to raise in the power of 2. I have tried:
for k=1:2;
for t=1:100;
A2{k,t}=A{k,t}.^2;
end;
end;
But it doesn't work. Thanks in advance.
  댓글 수: 2
Jay shankar Nanda
Jay shankar Nanda 2021년 3월 29일
how i CAn raise the power of an single element of the array
Jay shankar Nanda
Jay shankar Nanda 2021년 3월 29일
x={2,3,4};
>> x(2).^2
Undefined operator '.^' for input arguments of type 'cell'.
kindly tell how to access the input argumentand raise it to power

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

채택된 답변

James Tursa
James Tursa 2016년 11월 18일
편집: James Tursa 2016년 11월 18일
Like this?
A = 2x100 cell array of scalars
A2 = mat2cell(cellfun(@(x)x.^2,A),ones(1,2),ones(1,100));
SIDE NOTE:
Your posts seem to indicate that you have a lot of scalar data in cell arrays that you want to do basic matrix arithmetic/functions with. Is there a reason you have this data in cell arrays rather than double matrices? It would be a lot easier (and cleaner) to maintain this data as double matrices in your code rather than in cell arrays unless you have some specific reason to have the cell arrays.

추가 답변 (1개)

gsourop
gsourop 2016년 11월 18일
You are right. However, I needed to create cell arrays in an earlier stage in order to avoid tens of repetitions and I am stack with the cell arrays.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by