How to replace zeros in a matrix by elements of an array in Matlab?

In summary, the conversation discusses a solution for replacing ones and zeros in a matrix with elements of an array in a specific order. The participants suggest using a loop and a separate variable to achieve this.
  • #1
quetzal
6
0

Homework Statement


I have a matrix that contains zeros but it may contain also ones. For example:
Code:
                   ID=[1 0 0 0;
                       0 0 0 0;
                       1 0 1 1]
I'm trying to replace all the ones by zeros and all the zeros by elements of an array EA going column-wise so that the resulting matrix ID would look like:
Code:
EA=1:8;        ID=[0 2 5 7;
                   1 3 6 8;
                   0 4 0 0]

Homework Equations


The Attempt at a Solution


I tried it this way but don't know how to finish it:
Code:
D=3;
N=4;
ID=zeros(D,N);
equations=sum(ID(:)==0);% number of zeros in matrix ID
EA=1:equations;

for j=1:N
    for i=1:D
        if ID(i,j)==1
           ID(i,j)=0;
        else            
           ID(i,j)=?;           
        end
    end
end
 
Last edited:
Physics news on Phys.org
  • #2
you could make a separate variable say num for EA and do this
num=1;

for j=1:N
for i=1:D
if ID(i,j)==1
ID(i,j)=0;
else
ID(i,j)=EA(1,num);
num+=1;
end
end
end
 
  • #3
Awesome! I've got it done similarly...Thanks!
num=1;

Code:
for j=1:N
for i=1:D
if ID(i,j)==1
ID(i,j)=0;
else
ID(i,j)=EA(num);
num=num+1;
end
end
end
 

1. What is the best way to replace zeros in a matrix with elements from an array in Matlab?

The best way to replace zeros in a matrix with elements from an array in Matlab is to use the "find" function to locate the indices of the zero elements in the matrix, and then use those indices to replace the zeros with the desired elements from the array.

2. Can I replace zeros in a matrix with elements from an array without using a loop in Matlab?

Yes, it is possible to replace zeros in a matrix with elements from an array without using a loop in Matlab. This can be achieved by using the "find" function and the "isequal" function to locate the indices of the zero elements and replace them with the desired elements from the array.

3. How can I efficiently replace multiple zeros in a matrix with different elements from an array in Matlab?

To efficiently replace multiple zeros in a matrix with different elements from an array in Matlab, you can use the "find" function to locate the indices of the zero elements, and then use the "ismember" function and logical indexing to replace the zeros with the desired elements from the array.

4. Is it possible to replace only a specific number of zeros in a matrix with elements from an array in Matlab?

Yes, it is possible to replace only a specific number of zeros in a matrix with elements from an array in Matlab. This can be achieved by using the "find" function to locate the indices of the zero elements, and then using logical indexing to select only the desired number of zeros to be replaced with elements from the array.

5. Can I replace zeros in a matrix with elements from an array while also preserving the original matrix in Matlab?

Yes, it is possible to replace zeros in a matrix with elements from an array while also preserving the original matrix in Matlab. This can be done by creating a copy of the original matrix and then using the "find" function and logical indexing to replace the zeros with the desired elements from the array in the copied matrix.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
951
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
696
  • Engineering and Comp Sci Homework Help
Replies
4
Views
873
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top