简体   繁体   中英

creating a loop to convert elements of a matrix in matlab

Question: Create a copy of check called checkflip. Then, create a loop that converts each element of checkflip to 1 if its value is 0 and to 0 if its value is 1. The code within your loop should operate on only one element of checkflip at a time

In previous questions, I established check in the code below:

x=[0 1]; 
y=x([2 1]);  
check=repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 

I've tried the code below to solve my question but it doesn't seem to do anything and i am not sure why

checkflip=check; 
x=checkflip; 
for i=1:8
    if x(i)==0
        x(i)=1; 
    elseif x(i)==1
        x(i)=0; 
    end 
end 

Taking the complement using ~ is an option to flip each value in the matrix using a set of for-loops. A set of two for-loops can be used to traverse through each index of the array 1 by 1. The outer for-loop controls the scanning through the rows and the inner for-loop controls scanning through the columns. Alternatively, method 3 shows how to use a single loop and evaluate the corresponding row (i) and column (j) accordingly. It's important to be careful with consecutive if statement because sometimes within the first if statement's body checkflip can be flipped causing the second if statement to trigger accidentally. Sometimes it can be good practice not to involve the same variables in consecutive if statements and their bodies.

Edit: Alternatively you can use one for-loop that runs from 1 to the number of array elements in this case 64 since MATLAB index arrays by traversing the rows from left to right.

检查和检查翻转数组

Looping Methodology:

Below shows the looping method for a 5 by 5 array/matrix but this can easily be extended to arrays/matrices of any size. 循环图

Method 1: Using Complement

x=[0 1]; 
y=x([2 1]);  
check = repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 


checkflip = check; 

for i = 1: 8
   for j = 1: 8 
       
 checkflip(i,j) = ~checkflip(i,j);
       
   end
end

or

x=[0 1]; 
y=x([2 1]);  
check = repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 
checkflip = check; 

for i = 1: 64   
 checkflip(i) = ~checkflip(i);
end

Method 2: Using If Statements

x=[0 1]; 
y=x([2 1]);  
check = repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 


checkflip = check; 

for i = 1: 8
   for j = 1: 8 
       
 if(checkflip(i,j) == 0)
     Result = 1; 
 end
           
 if(checkflip(i,j) == 1)
     Result = 0;
 end
     
 checkflip(i,j) = Result;
 
   end
end

Method 3: Limited to One Loop

x=[0 1]; 
y=x([2 1]);  
check = repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 

checkflip = check; 

i = 1;
j = 0;
for Loop = 1: 64
j = j + 1;
    
    if(j > 8)
    j = 1;
    i = i + 1;
    end
        
 if(checkflip(i,j) == 0)
     Result = 1; 
 end
           
 if(checkflip(i,j) == 1)
     Result = 0;
 end
     
 checkflip(i,j) = Result;
 
end

Edit: A great solution suggested by @David uses the function numel() to transverse over all the array elements seamlessly:

x=[0 1]; 
y=x([2 1]);  
check = repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 

checkflip = check; 
for i=1:numel(checkflip)
    
checkflip(i) = ~checkflip(i);

end

Extension: Without a Loop (excepted from the question, all elements at once)

x=[0 1]; 
y=x([2 1]);  
check = repmat([repmat(y,1,4);repmat(x,1,4)], 4, 1); 
checkflip = double(~check);

Ran using MATLAB R2019b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM