Assign Value to Multiply Indexed Range

조회 수: 6 (최근 30일)
Bård Skaflestad
Bård Skaflestad 2019년 7월 16일
댓글: Bård Skaflestad 2019년 7월 16일
Suppose control is an m-element one-dimensional structure array such that each element contains another n-element one-dimensional structure array W, with a scalar (specifically, LOGICAL) field status. As a simple example consider the synthetic construction
control = repmat(struct('W', struct('status', { false, true, true, false, true })), [1, 5])
In other words, the expression
control(i).W(j).status
is valid for all i=1:5 and all j=1:4.
My question is if there is an easy way of assigning W(3).status for all i=3:5 (i.e., a specific j value for a subset of the i range) without using the obvious loop
for i = 3:5, control(i).W(3).status = false; end
I didn't see a good way of persuading deal to do this.
Note: I'm perfectly willing to change my data structure if that's what it takes to simplify the task. Maybe turning W into a matrix is the right choice here (e.g., [control.W(3:5,2).status] = deal(false)). In my general case, though, the number of W elements might be different for each value of i.

채택된 답변

Bruno Luong
Bruno Luong 2019년 7월 16일
편집: Bruno Luong 2019년 7월 16일
You might store in a table, which by design put the two variables/row dimensions to the same level; so you can index them either way.
The struct as you built is hierachical. The advantage is you can put scatted length data in the nested j-level. But it prevents user to assign the same nest index for subset of outer index.
c = repmat({[false;true;true;false;true]},1,5)
ControlStatus = table(c{:})
Assign
i = 3:5;
j = 3;
ControlStatus(j,i) = repmat({false},size(i))
  댓글 수: 1
Bård Skaflestad
Bård Skaflestad 2019년 7월 16일
> You might store in a table, which by design put the two variables/row dimensions to the same level; so you can index them either way.
Hm, that's an interesting thought. I haven't really used tables much, but this might be a good opportunity to learn and experiment with them.
> The struct as you built is hierachical. The advantage is you can put scatted length data in the nested j-level.
Truth be told, that was the original reason for using the hierarchical structure.
> But it prevents user to assign the same nest index for subset of outer index.
Absolutely, and that's the problem I'm running into now. I think it's a good time to rethink the greater task from the ground up. Thanks for the hint!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by