Need help subtracting certain vector indices from every matrix element in a page (without using for loop)

2 ビュー (過去 30 日間)
Hey! I have a problem that seems very easy, but for some reason I cannot manage to do it. (I don't want to use for loops thought, for optimization)
I have:
  • matrix of 15x15x500 containing random values, let's say:
A = randn(15x15x500)
  • vector containing different values of 1x500, let's say:
B = randn(1x500)
I want to subtract the values of the vector from every element of every page of the matrix that has the same index.
So something like this (which does not work of course):
C = minus(A,B,'all')
Meaning that if the page with index 450 of A would be all 15x15 of 5's, and the value of vector B with index 450 would be a 2, the 450th page of C would now be all 3's.
Is this possible using matrix operations and not a for loop?
Thanks in advance!

回答 (1 件)

Kevin Jansen
Kevin Jansen 2022 年 2 月 22 日
I just found it out!
a = randn(15,15,500)
b = randn(1,500)
c = reshape(a,[225 500])
d = c - b
e = reshape(d,[15 15 500])

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by