필터 지우기
필터 지우기

For loop, Brace indexing is not supported for variables of this type.

조회 수: 10 (최근 30일)
This is my for loop, I wasnt too sure how set up the for loop
All_Values = [ ElecsB; ElecsU; EarsB; EarsU; FaceB; FaceU; NeckB; NeckU];
for i = 1: length(All_Values)
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));
end
I am getting this error message. Can you please point me how to fix this error?
Brace indexing is not supported for variables of this type.
Error in Allmodels (line 18)
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));

채택된 답변

Star Strider
Star Strider 2022년 4월 29일
The ‘All_Values’ matrix is not a cell array, so you cannot use cell array indexing for it. (I am not exactly certain what it contains.)
If it is defined as:
[ ElecsB, ElecsU, EarsB, EarsU, FaceB, FaceU, NeckB, NeckU] = deal(rand(1,10)*35); % Create Unsupplied Variables
All_Values = { ElecsB; ElecsU; EarsB; EarsU; FaceB; FaceU; NeckB; NeckU }; % Define As Cell Array
for i = 1: length(All_Values)
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));
end
a = 8×1
0 0 0 0 0 0 0 0
With that change, it works.
.
  댓글 수: 4
Image Analyst
Image Analyst 2022년 4월 29일
OK, great, but you still might want to review the link I gave below. There is lots of other good stuff in the FAQ too besides that.
Star Strider
Star Strider 2022년 4월 29일
Note that length is a bit ambiguous with respect to cell arrays.
[ ElecsB, ElecsU, EarsB, EarsU, FaceB, FaceU, NeckB, NeckU] = deal(mat2cell(rand(10,32)*35, ones(1,10), 4*ones(1,8))); % Create Unsupplied Variables
All_Values = { ElecsB; ElecsU; EarsB; EarsU; FaceB; FaceU; NeckB; NeckU } % Define As Cell Array
All_Values = 8×1 cell array
{10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell}
[rows,cols] = cellfun(@size, All_Values)
rows = 8×1
10 10 10 10 10 10 10 10
cols = 8×1
8 8 8 8 8 8 8 8
for i = 1: length(All_Values)
La = cellfun(@gt,All_Values{i}(:,4), repelem({25},rows(1),1), 'Unif',0) % Logical Array
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));
end
La = 10×1 cell array
{[0 0 0 0]} {[0 0 1 0]} {[0 0 0 0]} {[1 0 0 0]} {[1 0 0 0]} {[0 0 0 0]} {[0 0 1 1]} {[0 1 0 0]} {[0 1 1 1]} {[0 1 1 0]}
Operator '>' is not supported for operands of type 'cell'.
I managed to get the comparison working and producing the ‘La’ logical array. I leave the rest to you, since I’m not certain what you want to do.
The error refers to the ‘a(:,i)’ assignment. That is solved in ‘La’ and since I do not understand what the ‘a(i,:)’ assignment does so I defer to you to implement it.
.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 4월 29일
All_Values is not a cell array. It is a normal double array (probably) so you should use parentheses.
See the FAQ for a description of cell arrays and when to use braces, parentheses, and brackets:

카테고리

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