Error: Assignment has more non-singleton rhs dimensions than non-singleton subscripts
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
I have the following matlab code in which I have 6 variables(=n_gc) with the min and max limit as "it.min_gc" and "it.max_gc" respectively. And each variable has 3(=M_g) values. From that I want to produce different combination i.e., as I have 729 combination (=3^6). So I used "ndgrid" operation and it produced the result which is stored in 6 variables(s1 u1 u2 ts vs vw) i.e., for example values stored in s1 is s1(1) to s1(729). But when I want to produce 729 combination again (i.e., when p=2) and try to store it, it shows the error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
for k = 1:p
for a = 1:n_gc
gc(k,a,:) = (it.min_gc(k,a):((it.max_gc(k,a)-it.min_gc(k,a))/...
(M_g-1)):it.max_gc(k,a));
end
[s1(k,:) u1(k,:) u2(k,:) ts(k,:) vs(k,:) vw(k,:)] = ndgrid(gc(k,1,:),...
gc(k,2,:),gc(k,3,:),gc(k,4,:),gc(k,5,:),gc(k,6,:));
end
Please can anyone help me to resolve this issue?
채택된 답변
- it appears to me that you would be better rewriting your gc setting code in terms of linspace()
- I think you might have to use squeeze() on each of the arguments to ndgrid(). This could, however, be avoided if you were to move k to the last subscript.
- ndgrid is not going to be producing vectors, but you are trying to store the output as vectors
- it might be easier to rewrite in terms of cell arrays, especially if you do not have a strong need to have individual names s1, u1, etc..
댓글 수: 14
Thank you. I used the linspace() for the gc setting code. I tried to use the squeeze() for each of the arguments to ndgrid(). It doesn't work. even tried to move k to the last subscript and again got the same error. Please can you explain more about your 4th comment about cell array? I used the ndgrid() to generate combinations of variables i.e., s1 u1 u2 ts vs and vw (each variable having 3 values equally spaced within the limits). The resulting grid for example s1(1) u1(1) u2(1) ts(1) vs(1) and vw(1) is the first grid combination and I will be using similar 729 grid points later for stage one (p=1). But for second stage p=2, I want to generate another set of 729 combination which is produced from the different regions of the 6 variables which I am not able to generate
ndgrid returns arrays with as many dimensions as there are number of variables. You cannot store those 6-dimensional arrays in a vector the way you are doing. You need to store them in an array, at least temporarily; once you have them temporarily stored you can reshape them in to vectors.
[s1t u1t u2t tst vst vwt] = ndgrid(gc(k,1,:), gc(k,2,:), gc(k,3,:), gc(k,4,:), gc(k,5,:), gc(k,6,:));
s1(k,:) = s1t(:); u1(k,:) = u1t(:); u2(k,:) = u2t(:);
ts(k,:) = tst(:); vs(k,:) = vst(:); vw(k,:) = vwt(:);
The squeeze() turns out not to be needed.
For using ndgrid with cells, see http://www.mathworks.com/matlabcentral/answers/27059-combinations-of-variables-and-step-sizing-for-creation-of-doe
I already tried the same method of storing them in an array temporarily and tried to reshape it. But It gave me the error "??? Subscripted assignment dimension mismatch.. error in s1(k,:) = s1t(:);
Please can you help me to resolve this issue?
For now, switch to
s1{k} = s1t(:); u1{k} = u1t(:); u2{k} = u2t(:);
ts{k} = tst(:); vs{k} = vst(:); vw{k} = vwt(:);
Then, after the k loop, look at the size() of each cell member of each of the variables, looking for one that is different. Then you should be able to track back to _why_ that one is different.
I checked it too but s1 is not the only one. Reshaping is not working for any of the 6 variables i.e, obtained [s1t u1t u2t tst vst vwt] cannot be reshaped into vectors s1(k,:) = s1t(:); u1(k,:) = u1t(:); u2(k,:) = u2t(:);
ts(k,:) = tst(:); vs(k,:) = vst(:); vw(k,:) = vwt(:);
Right, so switch to cell arrays at the moment and look at the sizes of s1{1} vs s1{2} and so on.
I switched to cell arrays. I am getting the following error message "??? Cell contents assignment to a non-cell array object." Also tried to store it into structure. Nothing is working fine for me. Please Help
Please show your current code.
for k = 1:p
for a = 1:n_gc
gc(k,a,:) = linspace(it.min_gc(k,a),it.max_gc(k,a),M_g);
end
[s1t u1t u2t tst vst vwt] = ndgrid(gc(k,1,:), ...
gc(k,2,:),gc(k,3,:),gc(k,4,:),gc(k,5,:),gc(k,6,:));
s1{k} = s1t(:);
u1{k} = u1t(:);
u2{k} = u2t(:);
ts{k} = tst(:);
vs{k} = vst(:);
vw{k} = vwt(:);
end
just a note, the inputs (p, n_gc,it.min_gc, it.max_gc, M_g) are working correctly
Are you running this as a script or in a function? If you were running it in a script, then because you would already have had the variables such as s1 and u1 in your workspace, you would have had trouble using those variables as cell arrays. You should use a function to protect against this kind of problem. Or at least at the top of your code, put in a "clear" statement
clear k a gc s1 u1 u2 ts vs vw
Its a function, I am using the inputs (said in my last comment) to run this function.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Lengths and Angles에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
