Replace elements of each matrix within a cell array

조회 수: 3 (최근 30일)
Matthew McIlroy
Matthew McIlroy 2023년 2월 17일
답변: Bhavana Ravirala 2023년 2월 17일
I have a cell array with matrices in each cell and I want to edit the same elements in each matrix. How do I do this?
The code below shows how it can be created using a for loop but I want to vectorise it if possible where nD is a constant and nn is a vector and changes for each cell
G=zeros(4,nD*nn);
G([1 3],1:nD:end)=1;
G([4 2],2:nD:end)=1;
  댓글 수: 1
the cyclist
the cyclist 2023년 2월 17일
Your example is confusing to me, because it shows neither a cell array nor a for loop.
Can you give a small but representative example of the input and output you are expecting? It might also be helpful to upload your cell array here, in a MAT file. You can use the paper clip icon in the INSERT section of the toolbar.

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

채택된 답변

Bhavana Ravirala
Bhavana Ravirala 2023년 2월 17일
Hi,
I understand that you want to vectorize cell fun assignment.
You can use the ‘cellfun’ to apply a function to each element of the cell array.
nD = 10;
nn = 10;
M = cell(100,1);
for i=1:100
M{i}=zeros(4,nD*nn);
end
disp(M{1});
M = cellfun(@(x)temp(x), M, 'UniformOutput',false);
disp(M{1});
If you were looking to improve the performance of the operation, you can use the parfor loop.
For more information, please refer the below link.

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by