Matrix must agree HELP
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
Subscript indices must either be real positive integers or logicals.
Error in Skeleton_NS_solver (line 559)
plot(y(:,max(length(y)/2,ceil(length(y)/2)))',pp(:,max(length(pp)/2,ceil(length(pp)/2))+0.5*Uuu(:,max(length(Uuu)/2,ceil(length(Uuu)/2))).^2)','k-','Linewidth',1.5)
2 件のコメント
Bob Thompson
2018 年 2 月 19 日
Could you include more of the code, specifically the area where the matrices are defined?
Also, it is easier to read if you could use the {}Code option to display the pasted code as regular coded format. Just makes it easier to see as separate lines, rather than a large block.
paula García Ruiz
2018 年 2 月 19 日
回答 (2 件)
Guillaume
2018 年 2 月 19 日
0 投票
y, pp, etc. are obviously at least 2D, so length of these can be the number of rows or columns depending on the matrix, whichever is the largest. So using length to filter the column sounds very iffy. No idea if that is the cause of the problem, but replacing length by size with an explicit dimension would be wise.
As per Bob comment, knowing the actual size of the matrices would help in diagnosing the true cause.
6 件のコメント
paula García Ruiz
2018 年 2 月 19 日
編集済み: Walter Roberson
2018 年 2 月 19 日
paula García Ruiz
2018 年 2 月 19 日
I wrote size with an explicit dimension. If that length is supposed to be the number of columns, then it should be size(y, 2).
I also don't really understand the purpose of
max(x/2, ceil(x/2)) %with x > 0
It's always going to be
ceil(x/2)
However, that's not the problem. The problem is clear now that we have the size of the variables, you're trying to add a 3x1 vector pp(:, 2) with a 4x1 vector 0.5*Uuu(:, 2).^2. Adding vectors of different length is not something that matlab (or I) knows how to do.
edit: actually, that's also not your immediate problem. Your immediate problem is that one of your closing bracket is misplaced and thus you're doing:
pp(:, 2 + 0.5*Uuu(:, 2).^2)
instead of
pp(:, 2) + 0.5*Uuu(:, 2).^2
which of course is most likely to result in non-integer indices.
Once you fix that problem you'll run into the dimension mismatch error.
paula García Ruiz
2018 年 2 月 19 日
編集済み: Walter Roberson
2018 年 2 月 19 日
paula García Ruiz
2018 年 2 月 19 日
編集済み: Walter Roberson
2018 年 2 月 19 日
Debugging by forum is not an efficient method. You would be better off using Jan's advice and using matlab's debugger.
You also need to start thinking about what you write
uut = [ones(1, N+1)*..., ones(N, 1)*...
The first expression is going to result in a matrix with 1 row, the second in a matrix with N rows. Of course they can't be concatenated horizontally since they don't have the same number of rows, just as the error message tells you.
And please, use the {}Format button when posting code. Can't you see that my post is a lot more readable than yours?
Jan
2018 年 2 月 19 日
You can examine the reason of errors using the debugger: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html. Eitehr set a breakpoint in the failing line or stop Matlab automatically:
dbstop if error
The check the parts of the failing command:
plot(y(:, max(length(y)/2, ceil(length(y)/2)))', ...
pp(:, max(length(pp)/2, ...
ceil(length(pp)/2)) + 0.5 * Uuu(:, max(length(Uuu)/2,ceil(length(Uuu)/2))).^2)', ...
'k-', 'Linewidth', 1.5)
This is a very ugly command. Prefer to split it into parts, because it is easier to read and to maintain. Use spaces around operators and after commas.
tmpx = y(:, max(length(y) / 2, ceil(length(y) / 2)))';
tmpP = max(length(pp) / 2, ceil(length(pp) / 2));
tmpU = max(length(Uuu) / 2, ceil(length(Uuu) / 2));
tmpy = pp(:, tmpP + 0.5 * Uuu(:, tmpU) .^ 2)';
plot(tmpx, tmpy, 'k-', 'Linewidth', 1.5)
Now it is easier to find the error.
0 件のコメント
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!