2017-03-11 6 views
1

Wie kann ich unter Berechnungen (Suche nach C) ohne "For-Schleife" tun?Zusammenfassen spezieller Spalten von zwei Matrix in Matlab

[4, 2, 1, 7; 
A = 0, 3, 4, 0; 
    8, 0, 10, 12; 
    11, 6, 2, 5]; 

    [1, 0, 0, 4; 
B = 0, 3, 2, 0; 
    5, 0, 8, 10; 
    7, 2, 1, 2]; 

C(i,j)= B(i,j-1) - B(i,j+1) + A(i,j+1); %if j is not equal to 4(number of columns) and it is not equal to 1 
C(i,j)= B(i,4) - B(i,j+1) + A(i,j+1); %if j is equal to 1 
C(i,j)= B(i,j-1) - B(i,1) + A(i,1); %if j is equal to 4(number of columns) 

Antwort

0

Sie ein Array angeben kann als Index zur gleichen Zeit auf mehreren Elementen zu arbeiten:

A=[4,2,1,7;0,3,4,0;8,0,10,12;11,6,2,5]; 
B=[1,0,0,4;0,3,2,0;5,0,8,10;7,2,1,2]; 
C = zeros(size(A)); 
C(:,2:end-1) = B(:,1:end-2) - B(:,3:end) + A(:,3:end); %if j is not equal to 4(number of columns) and it is not equal to 1 
j = 1; 
C(:,j)= B(:,4) - B(:,j+1) + A(:,j+1); %if j is equal to 1 
j = 4; 
C(:,j)= B(:,j-1) - B(:,1) + A(:,1); %if j is equal to 4(number of columns) 
Verwandte Themen