简体   繁体   English

Matlab:如何将向量应用于细胞中的每个向量?

[英]Matlab: How to Apply vector to each vector in cell?

if V = [3 11 20 31 40] and A=[1 2 3] , we can do V(A(:))=[3 11 20] . 如果V = [3 11 20 31 40]并且A=[1 2 3] ,我们可以做V(A(:))=[3 11 20]

How I can do that if A is a cell? 如果A是一个单元格,我该怎么办?

if A={[1 2 3],[2 5],[3 5]} , i want to have {[3 11 20],[11 40],[20 40]} 如果A={[1 2 3],[2 5],[3 5]} ,我想拥有{[3 11 20],[11 40],[20 40]}

Convert A into a matrix (two ways): A转换成矩阵(两种方式):

V = [3 11 20 31 40]
A=[1 2 3]
V(cell2mat(A))
V([A{:}])

If A is a cell array of indices, use 如果A是索引的单元格数组,请使用

cellfun(@(m) V(m),A,'UniformOutput',false)

使用V(cell2mat(A))=[3 11 20]V([A{:}])=[3 11 20]

but if A={[1 2 3],[2 5],[3 5]} and i want to have {[3 11 20],[11 40],[20 40]}? 但是如果A = {[1 2 3],[2 5],[3 5]}并且我想拥有{[3 11 20],[11 40],[20 40]}?

I don't think there is a way to properly vectorize this problem, but you can at least use cellfun to get it into one line. 我认为没有办法适当地向量化此问题,但是您至少可以使用cellfun将其合并为一行。 It's not going to be especially fast, but at least it's concise... ish. 它不会特别快,但至少是简洁的。

cellfun(@(x) V(x), A, 'UniformOutput', false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM