Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Hi!
I have been trying to convert a matlab function into mathcad 14. The function is attahced in matlab format.(lpsd.m)
During this convertion i have found some difficulties with a native matlab functions. The BSXFUN.
If you check that lpsg.m file you can see the bsxfun function inside a for cycle. Does anyone have any idea how can I solve this?
thanks for your time people!
Cheers!
Solved! Go to Solution.
Here are my attempts to duplicate bsxfun(@plus, A, B) and bsxfun(@times,A,B):
If you look up what BSXFUN in Matlab is supposed to do, you'll find that it applies the operator indicated with the first argument to the two arrays in the second and third arguments.
IF A and B are arrays of equal size (length), then Matlab:
C = BSXFUN(@plus, A, B)
would be in Mathcad:
C:= A + B.
Success!
Luc
Hi LucMeekes,
yes, i know what it does.
My problem is whenever A and B are matrices with difernet sizes. You can not sum or do anything like that.
You cannot (mathematically correctly) add matrices or arrays of different sizes. It makes no sense.
So make sure they are of equal size and shape before you do the operation.
Success!
Luc
Thanks for the support. Unfortunetly that's something i already did.
That line of thought leads into the Multidimentional arrays on mathcad and i cannot do more then 7 or 8.
cheers,
I am not well versed in MatLab, but isn't one of the benefits of BSXFUN that it is broadcasting, that is it expands as necessary singleton dimensions?
Is this the point of your question? Which kind of arrays are typically fed into the two occurrencies of BSXFUN in your program?
This would require some programming in Mathcad as otherwise Mathcad would throw an error.
I think this may be a start. Image here, trying to attach the file.
So I can't paste an image from the clipboard.
And you have to zip a file to post it--the app doesn't automatically create the zip.
THIS IS SOOOOO MUCH BETTER!
In Matlab your example would give an error as a 3 row matrix can't be blown up to a 4 row matrix.
Only a 1 row matrix is blown up to a multi-row matrix or a 1 column matrix (vector) is blown up to a multi-column matrix as necessary. Thats what singelton expansion (the s and x in bsxfun) means.
I guess its hard to duplicate "bsxfun" in Mathcad with a user function that accepts an arbitrary function handle as we had to consider which dimensions of input matrices are valid for the specific function - this differs.
If A is a 3x5 matrix and B is a 1x5 matrix, then using @plus B is expanded to a 3x5 matrix by duplicating (triplicating?) the first (and only) row and then then two 3x5 matrices are added.
If we use @times for the function handle, B is blown up to a 5*5 matrix so it can be multiplied with A.
In your example we could use A and B_transposed as arguments and both input arguments are expanded to 4x3 matrices. If we use A_transposed and B we get 3x4 matrices.
See here the output of MatLab:
And yes, you are right. Its soooo much more comfortable to embed pictures in posts. Just now I realize how much I missed that saving of pics to temporary files, clicking around to insert it and then deleting the temp file. Really a step forward ... in the wrong direction.
Here are my attempts to duplicate bsxfun(@plus, A, B) and bsxfun(@times,A,B):
You caught me; I was guessing at what the beast was supposed to do. And clearly, I did not understand. You're correct, a general function will be a lot more work.