fill a matrix within a loop

Hello everyone,
I am growing more confident in Matlab, thanks to community support, but sometimes I feel stucked due to very silly issues, as per the following case:
matr_counts_per_chunk = zeros(120,2752);
for col= 1:length(tr_x_ch)
matr_mat_fil_non_bool_sigle_tr = resh_matr_mat_fil_non_bool(:,col);
matr_counts_per_chunk(:,col) = sum(reshape(matr_mat_fil_non_bool_sigle_tr,[],250),2);
end
the problem is: matr_counts_per_chunk is only correctly filled in the first iteration, then I get only zeros in every 2751 left columns. This mean the for loop is actiually doing nothing/not working except for the first call.
Any help will be appreciated

7 Comments

What size does tr_x_ch have?
2752
Thanks for your help. I really need some advice as my work is stucked on this silly for loop
@Enzo you don't need to increment col at the bottom of the for loop. It's unnecessary since the for loop will do that for you, and in fact, whatever you do to col is only in effect during the loop and when the loop starts a new iteration, it makes col what it should be. Bottom line, don't change col in the loop.
If you have any more questions, then attach your data (resh_matr_mat_fil_non_bool and tr_x_ch ) and code to read it in with the paperclip icon after you read this:
save('answers.mat', 'resh_matr_mat_fil_non_bool', 'tr_x_ch');
Then attach 'answers.mat' with the paperclip icon.
@Stephen23 @Image Analyst sorry, I have uploaded the wrong code. That was just an "extreme" attempt after so many unsuccessfull ones. I have edited the code and this was my most reasonable try. In the attacments you shuld find the matrix in order to run the code yourself. Please help me in solving this annoying problem. Thanks again
@dpb hello there. The previous question was deleted being a duplicate question. by the way, i just checked and the matrix is not filled with empty values. More or less, there are values all along the second dimension (I just ramdomly checked several columns). In the attachemnts you will find the actual matrix in order to run the code yourself. Any help would be much appreciated
"In the attachemnts you will find the actual matrix in order to run the code yourself. "
Nope: you did not include tr_x_ch as Image Analyst requested, nor have you described its size as I requested.
Enzo
Enzo on 29 Apr 2023
Edited: Enzo on 29 Apr 2023
hi @Stephen23 , carefully cheking my first reply, you will find 2752 in the first line. Maybe it was not so clear
tr_x_ch = 2752 is the actual reply
I have attached the file to the question

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 29 Apr 2023
Edited: Stephen23 on 29 Apr 2023
"tr_x_ch = 2752 is the actual reply"
Then tr_x_ch is a scalar. The length of a scalar is exactly 1 (by definition), so your loop will iterate exactly one time.
If that is the case, you probably want this:
for col= 1:tr_x_ch

1 Comment

@Stephen23 that's exactly why I feel like it was really easy to solve, and I am still a noob in the way to become proficient in matlab. still a long way! Thanks so much

Sign in to comment.

More Answers (1)

Here's what I get, after adding comments and clarifying the variable names.:
fileName = 'resh_matr_mat_fil_non_bool.mat';
load(fileName) % Load "resh_matr_mat_fil_non_bool" from a .mat file.
% resh_matr_mat_fil_non_bool is a 30,000 row by 2752 column 2-D matrix.
[rows, columns] = size(resh_matr_mat_fil_non_bool)
tr_x_ch = zeros(rows, 1); % A 2752 column vector.
% Preallocate a matrix.
matr_counts_per_chunk = zeros(120, 2752);
for col = 1 : columns
% Get data into a 30,000 by 1 column vector.
thisColumn = resh_matr_mat_fil_non_bool(:, col);
% Reshape into 120 rows by 250 columns.
matrix2d = reshape(thisColumn, [], 250);
% Sum the matrix across, getting a 250 row by 1 column vector
% and put the sums into the column of matr_counts_per_chunk;
matr_counts_per_chunk(:, col) = sum(matrix2d, 2);
end
fprintf('Done!\n')
matr_counts_per_chunk has values all over the matrix, not just in the first column, so I can't reproduce what you said. It basically does what you tell it to.

1 Comment

@Image Analyst as @Stephen23 spotted in no time, my issue was way more stupid than one could thought. I was doing it wrong starting from the very first line. Sorry to wasted your time, but thanks for your efforts

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 29 Apr 2023

Commented:

on 29 Apr 2023

Community Treasure Hunt

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

Start Hunting!