MATLAB: Columns of a matrix

codecolumnsmatrix

I would like to know how to achieve one by one the columns of a matrix. Let me put here what I have done:
%COMBINATION OF ONE VARIABLE.
%we first read all the variables
Nby=53 ; % Le nombre des variables mesurées
y = xlsread('C:\Users\p105312\Desktop\dig+echg+washer+evap.xlsx' ,'E14:E66' ); % Les valeurs des variables mesurées
Nbz=5; % Le nombre des variables non mesurées
z =xlsread('C:\Users\p105312\Desktop\dig+echg+washer+evap.xlsx' ,'E75:E79' ); % Une estimation de la variable non mesurée extraite de la simulation
Nbc=15 ; % Le nombre de contraintes de systéme
Cp = xlsread('C:\Users\p105312\Desktop\dig+echg+washer+evap.xlsx' ,'D89:D94' ); % Les capacités calorifiques
y = xlsread('C:\Users\p105312\Desktop\dig+echg+washer+evap.xlsx' ,'E14:E66' ); % Les valeurs des variables mesurées
Ay = [ 1, 1, 1, 1, -1, -1, -1, -1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
; Cp(2)*y(12), Cp(4)*y(13), Cp(5)*y(14), Cp(3)*y(15), -Cp(5)*y(16), -Cp(3)*y(17), -Cp(5)*y(18), -Cp(5)*y(19), 0, Cp(5)*y(21), Cp(5)*y(22), Cp(2)*y(1), Cp(4)*y(2), Cp(5)*y(3), Cp(3)*y(4), -Cp(5)*y(5), -Cp(3)*y(6), -Cp(5)*y(7), -Cp(5)*y(8), 0, Cp(5)*y(10), Cp(5)*y(11), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
; 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 0, Cp(2)*y(49), Cp(5)*y(50), -Cp(1)*y(52), -Cp(5)*y(53), Cp(2)*y(45), Cp(5)*y(46), -Cp(2)*z(5), -Cp(1)*y(47), -Cp(5)*y(48)];
%———————————-
for j=1:Nby,
for i=1:Nbc,
A(i,j)=Ay(i,j);
end
A
end

Best Answer

  • To call the entire contents of a column of a matrix, use matrix name and the colon operator (:)
    Para encontrar la columna de una matriz, utilice el nombre de la matriz y el operador dos puntos (:)
    A = [1 2 3;...
    4 5 6;...
    7 8 9];
    A(:,1) = [1; 4; 7]
    A(:,2) = [2; 5; 8]
    and so on.
    The colon tells Matlab to take the data from every row giving you the entire colomn.. The second number says which column to take from. You can put things like 'i' from your loop in there too.