フィルターのクリア

Better way to keep dimensions using reshape from extracted struct array?

50 ビュー (過去 30 日間)
Javier Cuadros
Javier Cuadros 2024 年 8 月 20 日 8:00
コメント済み: Voss 2024 年 8 月 20 日 14:30
HEllo
I am wondering what is the best way to extract the data from an structured array in matlab and keeping is dimensionality if I use reshape while processing it.
Let's say I have the following struct array
load('data.mat')
myData.data.b
ans = 1x2
20.0183 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
20.0183 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
20.0182 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
20.0183 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 1x2
20.0182 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In order to extract all b values I would do
[myData.data(:).b]
ans = 1x10
20.0183 19.8833 20.0183 19.8833 20.0182 19.8833 20.0183 19.8833 20.0182 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But this rises the issue that the dimensionality of b is lost, since now it is treated as an vector. If I do reshape to try to recover that dimensionality I will get
bb=reshape([myData.data(:).b]',[],2)
bb = 5x2
20.0183 19.8833 19.8833 20.0183 20.0183 19.8833 19.8833 20.0182 20.0182 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
which is still not the original data, because all even row are swapped compared to original. So the only thing I came up was to undo that swapping on even rows via
bb(2:2:end,:) = fliplr(bb(2:2:end,:))
bb = 5x2
20.0183 19.8833 20.0183 19.8833 20.0183 19.8833 20.0182 19.8833 20.0182 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Hence I wonder whether there exists a better way to extract b from the struct array without having to do the reshape + even elemen flip.
thanks in advance,
Bes regards
  7 件のコメント
Javier Cuadros
Javier Cuadros 2024 年 8 月 20 日 10:38
thanks guys, Voss solution works! I didn't know that I could use it in that way!
Stephen23
Stephen23 2024 年 8 月 20 日 10:41
"I didn't know that I could use it in that way!"
You can use comma-separated lists with any function.

サインインしてコメントする。

採用された回答

Voss
Voss 2024 年 8 月 20 日 9:57
load('data.mat')
bb = vertcat(myData.data.b)
bb = 5x2
20.0183 19.8833 20.0183 19.8833 20.0182 19.8833 20.0183 19.8833 20.0182 19.8833
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 件のコメント
Javier Cuadros
Javier Cuadros 2024 年 8 月 20 日 10:38
Thanks @Voss this works !!
Voss
Voss 2024 年 8 月 20 日 14:30
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

arushi
arushi 2024 年 8 月 20 日 8:20
Hi Javier,
To extract data from a structured array in MATLAB while preserving its original dimensionality, you can use a combination of array manipulation functions that maintain the structure of your data. In your case, since each element of myData.data(:).b is a 1x2 vector, you can use reshape and cell2mat in a way that respects the original organization of the data.
Here’s a more efficient approach to extract b values from the struct array without manually flipping rows:
  1. Extract Data into a Cell Array: Use arrayfun to extract the data into a cell array, preserving the structure.
  2. Convert to a Matrix: Use cell2mat to convert the cell array into a matrix.
bMatrix = cell2mat(bCellArray');
bOriginal = reshape(bMatrix, [], 2);
Hope this helps.
  1 件のコメント
Javier Cuadros
Javier Cuadros 2024 年 8 月 20 日 10:03
HI Arushi, thanks for replying!
I don't understand fully step 1 because the extraction is the issue. Do you mean that my arrayfun needs to iterate over the struct array and convert it into a cell? Otherwise I always get only one element...
load('data.mat')
b1=arrayfun(@(x) x.data(:).b,myData,'UniformOutput',false)
b1 = 1x1 cell array
{[20.0183 19.8833]}

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by