Cell array multiplication error

조회 수: 5 (최근 30일)
Nevzat Can Yerlikaya
Nevzat Can Yerlikaya 2022년 6월 8일
댓글: Jan 2022년 6월 9일
Hi everyone, I have a code like this:
for i=1:18
for j=1:5
B{i,j}=AvgMatrix(i,j)*r_prob{i,j};
D{i,j}=B{i,j}.*r_sch{i,j};
end
Finalcell{i,1}=D{i,1}+D{i,2}+D{i,3}+D{i,4}+D{i,5};
Finalarray(i,:)=Finalcell{i,1};
end
in each r_prob cell, there is a 1000 randomly generated numbers. I run this code and it was successful and I close it down. However, when I try to run it again, it gaves me an error "Unable to perform assignment because brace indexing is not supported for variables of this type." for B{i,j}=AvgMatrix(i,j)*r_prob{i,j}. Does anyone know what is the problem? Any help would be appreciated. Thank you a lot.

채택된 답변

Jan
Jan 2022년 6월 8일
편집: Jan 2022년 6월 8일
The error message means, that either B or r_prob is not a cell array. Use the debugger to find out, what type it is instead. Type this in the command window:
dbstop if error
Run your code again, when it stops, check the variables:
class(r_prob)
class(B)
Is B pre-allocate as cell before?
Is this a script or a function? In a script you are using the workspace of the caller. So if B was created as a cell array before in the command window by accident, the code ran. But now with a clean workspace the code fails. This is the reason why scripts should be avoided in productive projects, because they cannot be debugged and tested exhaustively and have severe long range side effects.
  댓글 수: 2
Nevzat Can Yerlikaya
Nevzat Can Yerlikaya 2022년 6월 8일
Thank you so much. You are right I have used B not in this script but in another script and hence, it was in the workspace. I changed the letter and everything works smoothly. Thank you once again otherwise, I would not figure it out.
Jan
Jan 2022년 6월 9일
Because this is the typical problem of working with scripts, decide for using functions for all of your projects.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by