Convert rows an columns of 4D matrix into 2d matrix with rows and columns as index Ai_j

1 次查看(过去 30 天)
I have a matrix of radii' a=zeros(m,2^(m-1),n(1),it+1) and expressed as a(ii,jj,mm,j).
ii is the layer number,
jj is the row number,
mm is axial iteration index
j represents the time iteration.
How can I replicate the matrix a to the form ai_j(mm,j) with layers and rows as symbolic indeces. This would optimize the iterative process of my code and would be able to plot rows the matrix ai_j(mm,j) over a single loop over i and j.
I appreciate any suggestions or leads.
  2 个评论
Stephen23
Stephen23 2020-9-11
编辑:Stephen23 2020-9-11
"This would optimize the iterative process of my code..."
I doubt that it would optimize much. More likely the opposite would be the case.
"... and would be able to plot rows the matrix ai_j(mm,j) over a single loop over i and j."
Anything you can do with pseudo-indices in the variable name you can do better** with actual indices.
** better in the sense neater, simpler, faster, more efficient, less liable to bugs, easier to debug.
Hamad El Kahza
Hamad El Kahza 2020-9-11
Thank you for your valuable input. The idea is to solve for the radii' of a branching structure of pores. Looping over the plot would only necessitate one input (number of layers) to calculate the number of elements in the branch and their respective position. The second and main concern is when trying to plot directly through my 4D matrix, I get this error:
Error using plot
Data cannot have more than 2 dimensions.
Error in erosion_singfix2(line 310)
plot(a(2,1,x,1),' -- black ','Linewidth',2.05);
I could stick to the actual indices if I'm able to overcome this error, and plot directly from my original matrix.
I appreciate your help. Thanks!

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-9-11
编辑:Stephen23 2020-9-11
You will get an error because plot accepts matrices only (i.e. 3rd and higher dimensions must be scalar), but apparently you are providing an array with a non-scalar 3rd dimension.
Assuming x is some non-scalar index, then you can simply permute the array to get a matrix, e.g.:
m = permute(a(2,1,x,1),[3,4,1,2]);
plot(m,...)
  2 个评论
Hamad El Kahza
Hamad El Kahza 2020-9-11
编辑:Hamad El Kahza 2020-9-11
Thanks! This indeed overcome the plotting constraint. I would need to permute the array for different layers and columns, as well as different iteration instants (J is the overall time of the compilation).
I'm just wondering whether this is the optimal option if I am trying to plot at several instantaneous times as well e.g.
m = permute(a(1,1,x,floor(2*J/10)),[3,4,1,2]);
m1 = permute(a(1,1,x,J,[3,4,1,2]);
m2 = permute(a(2,1,x,floor(7*J/10)),[3,4,1,2]);
m2 = permute(a(2,2,x,floor(9*J/10)),[3,4,1,2]); ...
Stephen23
Stephen23 2020-9-11
"...whether this is the optimal option if I am trying to plot at several instantaneous times..."
That depends on factors that we don't know about: the size of the array, how much memory you have, how many times you need to plot the data, what else you are using the array for, etc.
You could permute the array once and switch around the indexing to suit, which might be more efficient:
b = permute(a,[3,4,1,2]);
plot(b(x,1,2,1))
...

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by