且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在MATLAB中将值分配给结构数组的字段

更新时间:2023-09-06 22:19:40

信用归@Slayton所有,但实际上您也可以使用

Credits go to @Slayton, but you actually can do the same thing for assigning values too, using deal:

[a([a.b]==1).b]=deal(3)

那么细分:

[a.b]

检索数组a的所有b字段,并将其放入逗号分隔列表.

retrieves all b fields of the array a and puts this comma-separated-list in an array.

a([a.b]==1)

使用逻辑索引仅索引a满足约束的元素.随后,上面的完整命令根据3分配给结果逗号分隔列表的所有元素.html#br2js35-8"rel =" noreferrer>此.

uses logical indexing to index only the elements of a that satisfy the constraint. Subsequently the full command above assigns the value 3 to all elements of the resulting comma-separated-list according to this.