Porting over some code to Python and I don't understand this multiplication syntax

1 Ansicht (letzte 30 Tage)
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!

Akzeptierte Antwort

Mootrax
Mootrax am 18 Feb. 2019
Well groovy pants to me! I solved it. In a way. Basically I had some of my matrices in the wrong shape. So I had to work that out and then my multiplier worked. It should be noted that a basic * multiplier in Numpy is not the same as the one in MATLAB.
In Numpy you have to use the dot method.
dstack = np.vstack(np.concatenate((d, np.array(1, ndmin=1)))) # MATLAB equiv: [d;1]
cmult = C.dot(dstack)
cref = np.array(C[:,0], ndmin=2).T
C = np.concatenate((cmult, cref), axis=1)
This is how I arrived at the expected figures. I could lose the two lines in the middle and I could probably simplify the dstack variable, but essentially it's working. I went snowblind last night on this, but managed to figure it out in a short time this morning.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 18 Feb. 2019
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.

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by