how insert array in field struct

Z is matrix number
for i=1:width(A)
A(i).b=z(:,i)
end
it's possibile to avoid loop?

 Risposta accettata

Yes, it is possible to avoid the loop and achieve the same result using matrix operations in MATLAB using the following snippet
% Assuming A is a structure array and z is a matrix
z = [1 2 3; 4 5 6; 7 8 9]; % Example matrix
% Convert z to a cell array of column vectors
zCell = mat2cell(z, size(z, 1), ones(1, size(z, 2)));
% Assign the columns of z to the field b of A using comma-separated lists
[A.b] = zCell{:};
A.b
ans = 3×1
1 4 7

3 Commenti

!!! WARNING !!!
If A does not exist in the workspace or if it is e.g. a scalar structure, then this code will make data disappear without any warning. This is shown in the author's own example: note how the 2nd and 3rd columns are not present in the expansion of A.b
Is this a better way, at least for the case where A does not already exist. I think it will also work if A already exists.
z = [1 2 3; 4 5 6; 7 8 9]; % Example matrix
zCell = mat2cell(z, size(z, 1), ones(1, size(z, 2))); % or use num2cell(z,1)
[A(1:size(z,2)).b] = zCell{:};
A.b
ans = 3×1
1 4 7
ans = 3×1
2 5 8
ans = 3×1
3 6 9
Stephen23
Stephen23 il 16 Lug 2023
Modificato: Stephen23 il 17 Lug 2023
"Is this a better way, at least for the case where A does not already exist."
In that case STRUCT would be simpler and makes the intent much clearer.
"I think it will also work if A already exists."
In that case fiddling around with linear/subscript indexing on the LHS is not required.

Accedi per commentare.

Più risposte (2)

Stephen23
Stephen23 il 13 Lug 2023
Modificato: Stephen23 il 13 Lug 2023
"it's possibile to avoid loop?"
Does A already exist or not? Your question does not make this clear... here are both cases:
Z = [1,2,3; 4,5,6; 7,8,9]
Z = 3×3
1 2 3 4 5 6 7 8 9
C = num2cell(Z,1);
A = struct('a',C) % structure does not exist
A = 1×3 struct array with fields:
a
[A.b] = C{:} % structure already exists
A = 1×3 struct array with fields:
a b
Checking:
A.b
ans = 3×1
1 4 7
ans = 3×1
2 5 8
ans = 3×1
3 6 9
Read more:
Rahul
Rahul il 13 Lug 2023
Hi Luca,
This is possible. You can try out the following code for same.
A = struct('b', cell(1, width(A))); % Preallocate struct array
% Assign columns of z to the field 'b' of each struct element in A
[A.b] = deal(z);
Here deal(z) is used to assign each column of z to the corresponding field b in each struct element of A. This way, you can avoid the need for a loop and achieve the desired assignment efficiently.
Make sure that the number of columns in z matches the number of elements in A (i.e., width(A) or numel(A)).
Hope this helps.
Thanks.

3 Commenti

shamal
shamal il 13 Lug 2023
Modificato: shamal il 13 Lug 2023
deal is not correct....i get 82x82..
instead I want to get 82x1
It's not clear if you want
  1. an array of structures (82 A's) with each structure having one field "b" which is a column vector of 82 elements, OR
  2. if you want a single structure (one A, not an array of 82 of them) and that one structure has a single field which is an 82 element column vector.
Your initial code seems to indicate 1 -- an array of structures, but is that really true?
Why do you want to avoid the loop anyway? With a microscopic data size like 82, a for loop may well be faster than messing around with inefficient cell arrays. Either way will be so fast you won't be able to perceive a difference between them anyway.
shamal
shamal il 13 Lug 2023
Modificato: shamal il 13 Lug 2023
the solution of stephen23 is correct!
C = num2cella(Z,1);
[Ab] = C{:} % la struttura esiste già

Accedi per commentare.

Richiesto:

il 13 Lug 2023

Modificato:

il 17 Lug 2023

Community Treasure Hunt

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

Start Hunting!

Translated by