??? Subscripted assignment dimension mismatch.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Getting the error in title, my code was:
>> P=zeros(3,3);
>> P(3,3)=p
Any idea why? I'm guessing I need to assign p to something. But p will be my input value for a function.
채택된 답변
Geoff Hayes
2014년 11월 23일
Fred - the error message is telling you that there is a dimension mismatch between the variables that are being assigned at
P(3,3) = p;
As P is a 3x3 matrix, then P(3,3) is a scalar (so 1x1 element). If p is a scalar, then the above will work. But if p is an array or matrix (so anything other than a 1x1) then you will observe this error.
For example, consider the following code
P=zeros(3,3);
p = 42;
P(3,3) = p; % scalar assignment works
p = [42 42];
P(3,3) = p; % assignment of vector to scalar fails
P(3,1:2) = p; % assignment of vector to a vector works
So the dimensions of what you are trying to assign (the "source") must match the dimensions of the "destination". So what are you trying to assign to this element of P? What are the dimensions of p?
댓글 수: 18
Thanks Geoff. I wish to define the p value as the output of a function. The input to this function will have 3 variables, and those 3 variables will come from a data source.
So the question is how do I tell Matlab that this p value is the output of a function, of which the inputs are from some data source?
But what do you expect p to be - a scalar, an array, or a matrix? You don't have to tell MATLAB that p is the output of a function, as you could easily do
P(3,3) = myFunc(A,B,C);
But the above will only work if the output from myFunc is a scalar.
I expect p to be a value between 0 and 1; a probability.
If I do it that way, it tells me the input variable A is undefined. As I said the inputs will be used from an external data source.
Fred - the above is just an example, so of course trying to execute it will generate an error. And myFunc is probably not even the name of a function that you have.
If p is in fact a value between 0 and 1, then calling
P(3,3) = p;
should work. But since it doesn't, it is because p is not a scalar like you think it is. Try calling
size(p)
before the assignment. What does this return?
size(p)
ans =
17 17
no I tried that with my own function, and it did not work either. size(p) gives above.
If the size of p is [17 17] how can you write?
P(3,3) = p
Sorry, let me clarify what I'm trying to do:
P = zeros(3,3)
generates a 3x3 matrix of zeros. Then,
P(3,3) = p
assigns the (3,3) element to be p. But error is I'm trying/need to define p. For example if I write:
P(3,3) = 1/2
instead, it returns a matrix of zeros with the (3,3) element being 0.5. But I want the (3,3) element to be p, which should be generated from a function with inputs from an external data set. Does that make sense?
You said the size of p is [17 17], that means it's not a single element, it 's a 17x17 matrix, you can't assign it to P(3,3)
I believe it said [17 17] as I was just experimenting around with something; irrelevant. I haven't assigned p to be anything yet, which is what I'm trying to do. I want to call the function to the element to the matrix P.
Azzi Abdelmalek
2014년 11월 23일
편집: Azzi Abdelmalek
2014년 11월 23일
To make things clears, post an example
function [p] = func1(a,b,c)
p = a*b + (1-a)*c;
end
Above is function I have written, then
P = zeros(3,3);
P(1,1) = 1;
P(2,2) = 1;
P(3,3) = p;
Then above is command to give matrix of zeros, with diagonal 1, 1 and p (which varies according to a,b and c)
You have to call p
a=1;
b=1;
c=1;
p=func1(a,b,c)
P(1,1) = 1;
P(2,2) = 1;
P(3,3) = p;
Right, in your code you have called p with your values of a, b and c (1, 1, 1). But my values of a, b and c vary according to some external data that I have. So how can I call p with that data?
What are these external data? We can't guess!
The data is just 20 different values for a, b and c. I have the data but I don't know which format it should be in. Excel etc.
Btw I may seem like a newbie to Matlab, but I have used Matlab about 18 months ago, so apologies if its not clear!
That means you are not asking the right question. Your problem is how to read those data. use xlsread function to read your data
Fred - what format is the data in now? If it is just in a text file, perhaps a comma separated file with three columns for a, b, and c, then you can use importdata or csvread. Start with this.
Once you have read in your data, presumably into a 20x3 matrix, you can then iterate over each row of the matrix and extract the three different values. You would then pass these three values into your function, and store the result somewhere. Whether you would "store" this in P(3,3) remains to be seen. Perhaps you will have 20 such P matrices.
Yes, 20 different matrices of P. I'll have a crack at that and see what happens. This is the beginning of my code so I'll probably be posting more questions!
Thanks Geoff
추가 답변 (1개)
Azzi Abdelmalek
2014년 11월 23일
This is not clear what you want to do. You have to assign some value to p
p=4
P=zeros(3,3);
P(3,3)=p
댓글 수: 1
It's not clear what you want, maybe you need to use cell array
p=[2 4;8 7];
P=cell(3,3);
P{3,3}=p
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
태그
참고 항목
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)
