Clear Filters
Clear Filters

For loop to create a matrix array

67 views (last 30 days)
Cutie
Cutie on 6 Feb 2022
Commented: Image Analyst on 21 Feb 2023
I have a data X say
X= 1:100 (integer 1 to 100)
I want to write a for loop that will create matrix of w=10x5 (10rows, 5 columns) where by column 1 will be the first 10 elements of X (1 to 10), column 2 (11 to 20) and so on and then stop at column 5 (41 to 50) before creating another matrix say w2 (ie starting the iteration again).
I don't want to use reshape function as it won't fit into the real data I am working on.
  4 Comments
Image Analyst
Image Analyst on 6 Feb 2022
Edited: Image Analyst on 6 Feb 2022
For 90, what are the sizes of the two matrices?
  1. 9-by-5? (in which case, my answer below works)
  2. Or one matrix of 10-by-5 and the second one of 10-by-4?
Cutie
Cutie on 7 Feb 2022
I appreciate @Matt J and @Image Analyst for offering your help.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 6 Feb 2022
Edited: Matt J on 6 Feb 2022
Because, I may need to create matrix of size 9 by 5. In which case I will only have 2 matrices formed from the first 90 elements (1:90) and the last 10 element can be discarded.
That doesn't disqualify reshape.
rows=9;
cols=5;
X=1:100;
M=rows*cols;
N=numel(X);
X=reshape( X(1:floor(N/M)*M) ,rows,cols,[]);
w=squeeze(num2cell(X,[1,2]))
w = 2×1 cell array
{9×5 double} {9×5 double}
  1 Comment
Cutie
Cutie on 7 Feb 2022
Thank you very much @Matt J. This works and it opens my eyes to other things.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 6 Feb 2022
Edited: Image Analyst on 6 Feb 2022
Try this
N = 100; % or 90 or some other multiple of 10
X= 1 : N; % (integer 1 to 100)
x1 = reshape(X(1:N/2)', N/10, [])
x1 = 10×5
1 11 21 31 41 2 12 22 32 42 3 13 23 33 43 4 14 24 34 44 5 15 25 35 45 6 16 26 36 46 7 17 27 37 47 8 18 28 38 48 9 19 29 39 49 10 20 30 40 50
x2 = reshape(X(N/2+1:end), N/10, [])
x2 = 10×5
51 61 71 81 91 52 62 72 82 92 53 63 73 83 93 54 64 74 84 94 55 65 75 85 95 56 66 76 86 96 57 67 77 87 97 58 68 78 88 98 59 69 79 89 99 60 70 80 90 100
If you don't want to use reshape for some weird reason, you can do it with a for loop but it will be more complicated.
X = 1 : 100;
x3 = zeros(10, 5);
x4 = zeros(10, 5);
k = 1;
for col = 1 : 5
for row = 1 : 10
x3(row, col) = X(k);
x4(row, col) = X(k + 50);
k = k + 1;
end
end
x3
x3 = 10×5
1 11 21 31 41 2 12 22 32 42 3 13 23 33 43 4 14 24 34 44 5 15 25 35 45 6 16 26 36 46 7 17 27 37 47 8 18 28 38 48 9 19 29 39 49 10 20 30 40 50
x4
x4 = 10×5
51 61 71 81 91 52 62 72 82 92 53 63 73 83 93 54 64 74 84 94 55 65 75 85 95 56 66 76 86 96 57 67 77 87 97 58 68 78 88 98 59 69 79 89 99 60 70 80 90 100
  3 Comments
Image Analyst
Image Analyst on 21 Feb 2023
@Navita as you can see above there is an output. I ran it in the editor with the green run triangle and there is definitely an output. Just look at my Answer and you can see it.
If you don't have any output then you must have copied my code and changed it somehow.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!