Porting over some code to Python and I don't understand this multiplication syntax
显示 更早的评论
Heya,
So I've been porting over the RAT.m file for my personal use. I'm porting a larger library over to Python that uses some built-in Matlab functions that SciPy and Numpy do not have. RAT.m is one of them. So I've done the bulk of it, except the following line is confusing me.
C = [C*[d;1] C(:,1)];
I know that probably seems basic as hell, but I'm going snowblind looking at it here.
The second bit:
C(:,1)]
I understand that. It translates to Python as:
C[:,0]
Very straight forward.
I even understand this bit:
[d;1]
And the way I'm translating that is like this in Python:
(np.vstack(np.concatenate((d, np.array(1, ndmin=1)))))
It's really this bit that I am confused by:
C*[d;1]
I don't understand what the multiplication is doing. I'm new to MATLAB but not new to Python. I think this is just one of those syntax things that I'm just not seeing. Can anyone help please?
Thank you!
采纳的回答
更多回答(1 个)
Walter Roberson
2019-2-18
1 个投票
It is not anything special.
In order for [d;1] to work, d and 1 would have to have the same number of columns. Since 1 has one column, then d must be a column vector. [d;1] then puts 1 at the end of it, for whatever reason.
C*[d;1] can only work if the number of columns in C is the same as the number of rows in [d;1] or [d;1] is a scalar. If d were empty then [d;1] would be the same as [1] and C*[d;1] would be the same as C. But there is not much point in computing that if you know it to be true, so we should assume that [d;1] is a column vector with the same number of rows as C has columns. That would then imply that d is a column vector with one fewer row than C has columns.
This would be like multiplying C by broadcast of [d;1].' and sum the results across the second dimension.
You might do something like this if you were fixing an intercept, or if you were fixing an axis of a rotation matrix.
There is no particular significance to [d;1], just a column of values having a 1 placed at the bottom.
Your expression to do the concatenate looks more complicated than is perhaps needed, but I do not have a lot of experience with python.
类别
在 帮助中心 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!