Initialize field values in a structure + create a list of zeros(10,10)

조회 수: 12 (최근 30일)
The Gogo
The Gogo 2021년 2월 9일
편집: The Gogo 2021년 2월 9일
Hi everyone, I want to initialize the same field for several indexes in a structure by some zeros(10,10), and I can't figure out how.
Already took me a while to figure out how to just initialize them to 0:
[test(5:9).field] = deal(0,0,0,0)
Which works but
  • I don't want to initialize by a scalar zero but by a 10x10 zeros matrixes
  • And obviously I don't want to repeat the
zeros(10,10)
as many times as I have indexes to initialize.
All the options I find are some kind of concatenation functions (for example repmat) which will obviously won't do the job.
There must be an easy an elegant way, but I can't seem to find it :/
Thanks in advance!
P.S.: and obviously, I'm not looking for a loop.

채택된 답변

The Gogo
The Gogo 2021년 2월 9일
편집: The Gogo 2021년 2월 9일
CORRECTION: actually the correct answer is in the comments of this answer. by Stephen Cobeldick.
test2 = cell(4,1);
test2(:) = zeros(10,10);
[test(5:9).field] = deal(test2);
Sorry for disturbing. I hope my struggle might be useful to some other people. If anyone has a shorter way, I'm still interested.
  댓글 수: 3
Stephen23
Stephen23 2021년 2월 9일
Simpler without the cell array:
mat = zeros(10,10);
[test(5:9).field] = deal(mat)
test = 1x9 struct array with fields:
field
test(4).field
ans = []
test(5).field
ans = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The Gogo
The Gogo 2021년 2월 9일
편집: The Gogo 2021년 2월 9일
Cool, thx! Actually this is the only right answer, mine does something else.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by