0
votes

I'm trying to extract elements from the first column of the following matrix using a for loop [1.0000 1.0000; 0.4401 4.0000; 0.0000 2.0000; 0.0000 3.0000] But I'm getting inaccurate values for the zero ones (example: 6.2421e-010 instead of zero) How to fix this?

Code:

for h=1:K 
  summation=0; 
  for i=1:F 
   x(i,1) 
   summation=summation+x(i,1); 
  end
end
3
Please post the code - Dan
code for h=1:K summation=0; for i=1:F x(i,1) summation=summation+x(i,1); end end - Amira Akra
But please note that x is an output of another function - Amira Akra
@AmiraAkra can u be clear where you face the problem? - lakshmen
@AmiraAkra What's the summation part for, are you just trying to extract or are you summing as well. Please define your question fully - Dan

3 Answers

0
votes

You don't need a for loop to extract the first column. You could do this:

a = [1.0000 1.0000; 0.4401 4.0000; 0.0000 2.0000; 0.0000 3.0000];
b = a(:,1);

Giving:

b =

    1.0000
    0.4401
         0
         0
0
votes

From what I have read so far:

A = [1.0000 1.0000; 0.4401 4.0000; 0.0000 2.0000; 0.0000 3.0000];
B = A(:,1);

No for loops needed..

if vpa(x(i,1)) < 0.0000003
  summation=summation+x(i,1);
else
 summation=summation+vpa(x(i,1));
0
votes
  1. If your matrix numbers are integers you dont need to write all the zeros after the dot.
  2. To extract a column of a matrix use M(:,j) where M is the matrix and j the column you want to extract.
  3. If you want to sum the elements of that column, just do sum(M(:,j))

    M= [1 1; 0.4401 4; 0 2; 0 3]
    
    c1=M(:,1)
    
    summation = sum(c1)