# Conditioning a matrix for loop

1 vue (au cours des 30 derniers jours)
Santos García Rosado le 16 Fév 2021
Hello Mathworks community! Could someone give me a hand?
I'm having trouble trying to create a matrix for loop. Since my actual code is complex, I'm going to propose a much simple idea.
Imagine I have a matrix A 4x12 input such as:
A = [1,2,3,4,5,6,3,5,4,2,5,1;
1,2,3,4,5,6,3,5,4,2,5,1;
12,5,6,9,2,3,5,3,4,3,6,1;
12,5,6,9,2,3,5,3,4,3,6,1]
And another matrix b:
b = [1, 2, 3;
2, 2, 1;
3, 2, 1;
1, 2, 6]
Let's say each row of A is divided in 3 subarrays with 4 positions each (12 positions in every row). Now, I'd like to sum each A position with the corresponding value of b. So b(1,1) would be added to A(1,1), A(1,2), A(1,3) and A(1,4); b(1,2) would be added to A(1,5), A(1,6), A(1,7) and A(1,8); ...
On the same loop, I would like to substract to every value of that output( the dimensions still are 4x12) the value of c corresponding to its row. (the first row of out minus c(1), the second row of out minus 12, ...
c = [10;12;14;16]
I'm working with much bigger data, so trying to do it "manually" wouldn't be useful.
Thank's for the help!
Santos
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Jon le 16 Fév 2021
You can do this without a loop, for example
out = [A(:,1:4)-b(:,1) A(:,5:8)-b(:,2) A(:,9:12)-b(:,3)]
out = out - c
##### 2 commentairesAfficher AucuneMasquer Aucune
Jon le 16 Fév 2021
If you have a large number of subarrays, perhaps that one line of code would get very long, and might be awkward to define. In this case you could use a loop like this. There may also be some other more clever way to vectorize (avoid using loops) this, which others might suggest. I don't do any checking in my example but you should make sure that the number of subarrays evenly divides the number of columns in A
numSub = 3; % number of subarrays
A = [1,2,3,4,5,6,3,5,4,2,5,1;
1,2,3,4,5,6,3,5,4,2,5,1;
12,5,6,9,2,3,5,3,4,3,6,1;
12,5,6,9,2,3,5,3,4,3,6,1]
b = [1, 2, 3;
2, 2, 1;
3, 2, 1;
1, 2, 6]
c = [10;12;14;16]
% find problem dimensions
numCol = size(A,2); % number of column in overall array
% determine number of columns in each sub array
numSubCol = numCol/numSub;
% make loop to build up new overall array from subarrays
out = zeros(size(A)); % preallocate
startIdx = 1:4:numCol;
endIdx = numSubCol:4:numCol;
for k = 1:numSub
out(:,startIdx(k):endIdx(k)) = A(:,startIdx(k):endIdx(k)) - b(:,k);
end
% subtract off c from each row
out = out - c;
Santos García Rosado le 17 Fév 2021
Thank's Jon. As you said, I have very long arrays so I'm forced to use a loop. I'll try to translate your code to mine and chek if it works.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by