Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

arrays - Convert Matlab grid to a vector

In Matlab I have 5D-grid X with dimensions lengths n1,n2,n3,n4,n5. I am fixing the first three dimension points at x1,x2,x3 and consider X(x1,x2,x3, , ). Now I would like to convert this to a n4 times n5 vector so that I can multiply it with another n4 times n5 vector. How can I do this?

Alternatively is there a direct way to do this multiplication without first writing a step where I convert the grid to a vector?

question from:https://stackoverflow.com/questions/65651192/convert-matlab-grid-to-a-vector

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if this is element-wise multiplication, and say Y is the n4*n5 vector:

A=bsxfun(@times,reshape(X,n1,n2,n3,[]),permute(Y(:),[4,3,2,1])));

will give you the result of such multiplication , where A is a 4D array of length n1,n2,n3,n4*n5.

The function reshape is used to convert X from 5D to 4D, and the function permute is used to "push" the information of Y into the 4th dimension for the multiplication to be done using bsxfun.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...