You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to convert 4D double matrix into 2D arrays
37 views (last 30 days)
Show older comments
Hi
I'm sorry for my English :(
I'm trying to convert 4-D double matrix into 2D arrays
There are a total of 14 different scenarios, each represented by a matrix of size 1x101 in the program. The representation in 4D is structured as (:,:,1,1) through (:,:,101,1) for the first scenario, (:,:,1,2) through (:,:,101,2) for the second scenario, and so on, up to (:,:,1,14) through (:,:,101,14) for the fourteenth scenario. Each set of indices within the 4D structure corresponds to a unique value in the respective scenario.
Thank you so much
8 Comments
Rik
on 13 Nov 2023
How exactly to you want to put that into 2D? Or do you perhaps mean you want to loop through all 1414 combinations and do some calculation with the 2D part?
Sung Wook Choi
on 13 Nov 2023
I want a 101x1 matrix for each of the 14 scenarios, where there are 14 different radii, with a sigma(1) for each wavelength (101).
Dyuman Joshi
on 13 Nov 2023
Edited: Dyuman Joshi
on 13 Nov 2023
"There are a total of 14 different scenarios, each represented by a matrix of size 1x101 in the program."
Then there should be a 3D array, 1x101x14 or 14x1x101. Where did the 4th dimension come from?
Is it a singleton dimension? In that case, what is the size of the 4D array?
"I want a 101x1 matrix for each of the 14 scenarios"
Why not store it as 101x14 matrix and use indexing to access the data?
Could you attach the data for the 4D array?
Sung Wook Choi
on 13 Nov 2023
Yes, I believe it's a singleton dimension. I'm also curious about why it's portrayed as a 4D double array. How can I verify its size? It's currently labeled as a 4D double array.
Dyuman Joshi
on 13 Nov 2023
"I'm also curious about why it's portrayed as a 4D double array."
Most likely, because that's how it was defined.
"How can I verify its size?"
Use size
Sung Wook Choi
on 13 Nov 2023
Do you mean 'using size' to verity its size?
I used sz=size( ), and it says
sz=1 1 101 14
Dyuman Joshi
on 13 Nov 2023
Yes, that's what I meant.
Ok, there are 2 singleton dimensions.
Now, What is the output you want? A 101x14 array?
Accepted Answer
Dyuman Joshi
on 13 Nov 2023
in = rand(1,1,101,14);
size(in)
ans = 1×4
1 1 101 14
out = squeeze(in);
size(out)
ans = 1×2
101 14
10 Comments
Sung Wook Choi
on 13 Nov 2023
Thank you so much! However, I have one question. After creating a matrix of size 101 x 14 using commands, do I need to manually copy/paste and assign values for each element? Or can I input the command directly into the existing matrix, and will it automatically reshape into a 101 x 14 format?
And when I try to simply copy/paste a 4D double matrix, there is a following message. What does it mean to use a cell array instead?
"You cannot paste data from the clipboard into an array. Note that MATLAB numeric arrays cannot contain text. If you want to paste text into an array, consider using a cell array instead."
Dyuman Joshi
on 13 Nov 2023
"Or can I input the command directly into the existing matrix"
What is the command that you are trying to use?
"And when I try to simply copy/paste a 4D double matrix"
From where are you trying to copy the data?
Sung Wook Choi
on 13 Nov 2023
1) I meant the command is sigma.
I want to focus on sigma(#1) of each wavelength(#101) for different 14 radiis(#14)
2) It's also the sigma that I want to copy.
ex) in sigma(4-D double), val(:,:,1,1) = 5.6851e-15 / val(:,:,2,1)= 5.6722e-15 ,,,,
the matrix is written in this way, and numerical values represent the desired sigma for me.
Dyuman Joshi
on 13 Nov 2023
I don't think I understood what you said.
Could you attach the data? Use the paperclip button to attach.
Sung Wook Choi
on 13 Nov 2023
sorry for my expression ,,
Here it is! I want to deal with 'sigma' inside the file 'scat_amor_sweep'
Dyuman Joshi
on 13 Nov 2023
I hope this is clear now -
load('scat.mat')
%Check the contents of the array
scat_amor_sweep
scat_amor_sweep = struct with fields:
Lumerical_dataset: [1×1 struct]
f: [101×1 double]
lambda: [101×1 double]
radius: [3.5000e-08 4.0000e-08 4.5000e-08 5.0000e-08 5.5000e-08 6.0000e-08 6.5000e-08 7.0000e-08 7.5000e-08 8.0000e-08 8.5000e-08 9.0000e-08 9.5000e-08 1.0500e-07]
sigma: [1×1×101×14 double]
%Assign the data to a variable
in = scat_amor_sweep.sigma;
size(in)
ans = 1×4
1 1 101 14
%Remove the singleton dimensions from the variable
out1 = squeeze(in)
out1 = 101×14
1.0e-12 *
0.0057 0.0084 0.0119 0.0163 0.0217 0.0283 0.0352 0.0424 0.0495 0.0561 0.0627 0.0692 0.0753 0.0880
0.0057 0.0083 0.0117 0.0159 0.0212 0.0277 0.0347 0.0419 0.0491 0.0558 0.0625 0.0690 0.0753 0.0881
0.0057 0.0082 0.0116 0.0156 0.0208 0.0271 0.0341 0.0414 0.0487 0.0555 0.0622 0.0689 0.0751 0.0882
0.0057 0.0082 0.0115 0.0154 0.0204 0.0266 0.0336 0.0410 0.0483 0.0551 0.0620 0.0687 0.0750 0.0882
0.0057 0.0081 0.0113 0.0151 0.0199 0.0261 0.0330 0.0404 0.0479 0.0548 0.0617 0.0684 0.0749 0.0882
0.0057 0.0081 0.0112 0.0149 0.0195 0.0255 0.0324 0.0399 0.0474 0.0545 0.0614 0.0682 0.0747 0.0882
0.0058 0.0081 0.0111 0.0147 0.0192 0.0250 0.0319 0.0394 0.0470 0.0541 0.0611 0.0680 0.0745 0.0882
0.0059 0.0081 0.0110 0.0145 0.0188 0.0245 0.0313 0.0389 0.0465 0.0537 0.0608 0.0677 0.0743 0.0880
0.0060 0.0081 0.0110 0.0143 0.0185 0.0240 0.0307 0.0383 0.0461 0.0534 0.0605 0.0674 0.0740 0.0879
0.0061 0.0082 0.0109 0.0142 0.0182 0.0235 0.0300 0.0377 0.0456 0.0531 0.0602 0.0672 0.0738 0.0877
You can combine the above steps as well -
out2 = squeeze(scat_amor_sweep.sigma)
out2 = 101×14
1.0e-12 *
0.0057 0.0084 0.0119 0.0163 0.0217 0.0283 0.0352 0.0424 0.0495 0.0561 0.0627 0.0692 0.0753 0.0880
0.0057 0.0083 0.0117 0.0159 0.0212 0.0277 0.0347 0.0419 0.0491 0.0558 0.0625 0.0690 0.0753 0.0881
0.0057 0.0082 0.0116 0.0156 0.0208 0.0271 0.0341 0.0414 0.0487 0.0555 0.0622 0.0689 0.0751 0.0882
0.0057 0.0082 0.0115 0.0154 0.0204 0.0266 0.0336 0.0410 0.0483 0.0551 0.0620 0.0687 0.0750 0.0882
0.0057 0.0081 0.0113 0.0151 0.0199 0.0261 0.0330 0.0404 0.0479 0.0548 0.0617 0.0684 0.0749 0.0882
0.0057 0.0081 0.0112 0.0149 0.0195 0.0255 0.0324 0.0399 0.0474 0.0545 0.0614 0.0682 0.0747 0.0882
0.0058 0.0081 0.0111 0.0147 0.0192 0.0250 0.0319 0.0394 0.0470 0.0541 0.0611 0.0680 0.0745 0.0882
0.0059 0.0081 0.0110 0.0145 0.0188 0.0245 0.0313 0.0389 0.0465 0.0537 0.0608 0.0677 0.0743 0.0880
0.0060 0.0081 0.0110 0.0143 0.0185 0.0240 0.0307 0.0383 0.0461 0.0534 0.0605 0.0674 0.0740 0.0879
0.0061 0.0082 0.0109 0.0142 0.0182 0.0235 0.0300 0.0377 0.0456 0.0531 0.0602 0.0672 0.0738 0.0877
Sung Wook Choi
on 13 Nov 2023
I really appreciate your help! Thanks to you, I've achieved the desired results. Additionally, do you happen to know a more efficient way to convert such a matrix to a text file? Currently, I'm copying/pasting the matrix from 'out2' to Excel and then copying/pasting it again into a text file. I'm curious if there's a faster and more efficient method.
Sung Wook Choi
on 15 Nov 2023
Thank you so much! Finally, may I ask what the difference is between the 'out1' and 'out2' matrices? They appear to be the same, and I couldn't identify any distinctions.
Also, when you use 'squeeze,' it removes two singletons. Does 'squeeze' mean removing all singletons in a matrix?
Dyuman Joshi
on 15 Nov 2023
You are welcome!
Yes, they are the same.
For the second one, I have just combined all the steps in one command.
Yes, 'squeeze' removes all the singleton dimensions of an array.
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)