# Issue with different length of columns in a matrix.

1 view (last 30 days)

Show older comments

x,y and z coordinates of a particular image has been saved as a matrix, by using the following code:

[x,y]=find(any(A,3)); % A is the variable which x and y coordinates are saved in.

xyz = [x, y];

xyz(:,1) =xyz(:,1).*0.0003; % multiplied by a calibration factor

xyz(:,2) =xyz(:,2).*0.0003; % -do-

xyz(:,3) = 2; % add the z coordinate to the matrix

save('A_new.mat', 'xyz','-v7','-append') % save the xyz variable

Here is the output which i am getting for the above code:

X extent: 0 to 1576

Y extent: 0 to 1894

Z extent: 0 to 9

Note: z column is added to the x and y column by appending it. My question is why i am getting a low length for the z column (why is it upto 9 instead of being 1576) Can anyone help me to resolve this matter?

##### 6 Comments

Image Analyst
on 8 May 2016

Column 3, which you consider as Z, is the same length as the first two columns, not different. You do not show us the code that generated these lines in the command window:

X extent: 0 to 1576

Y extent: 0 to 1894

Z extent: 0 to 9

So what Walter says still holds. There is no line that some something like

extentHighZ = ....whatever....

fprintf('X Extent: %d to %d', extentLowX, extentHighX);

fprintf('Y Extent: %d to %d', extentLowY, extentHighY);

fprintf('Z Extent: %d to %d', extentLowZ, extentHighZ);

### Accepted Answer

Image Analyst
on 8 May 2016

You made the extremely common beginner mistake of thinking row,column is the same as x,y. It is NOT. You should have

[y, x] = find(any(A, 3));

Look up find() the in help and you'll see it returns [rows, columns], which is [y, x], not [x,y].

##### 4 Comments

Image Analyst
on 8 May 2016

### More Answers (1)

dpb
on 7 May 2016

Edited: dpb
on 7 May 2016

"length of the columns of the matrix(eg: x & y columns) vary" is not so; there will be however many of each as there are number of non-zero locations that are nonzero across all plances in A, but whatever that number is, it will be identical for every A (there simply can't be a row coordinate without it's corresponding column, iow). Hence, the third column will also be the same length as you've assigned it to all rows in the combined array via the colon operator for the rows. The end dimension of xyz is length(x)*3 == length(y)*3.

Now, A may have different dimensions in X and Y, but that's not reflected in the length of the locations but in the possible magnitude of the result for the two columns.

##### 2 Comments

dpb
on 8 May 2016

There's no way the code snippet shown can generate that output; the variables in the code are x, y, and xyz from the 3D array A.

Perhaps A contains those values in each dimension; perhaps there's some function that writes the equivalent of the output of size() or somesuch but we can't see that from what you've posted; I've no klew what gives the above message, sorry.

I presume the answer is related to the size(A) but what precisely "extent" refers to here is unknown.

And, no, the expression length(x)*3 == length(y)*3 doesn't do any such thing, I was simply writing that as your code exists the two array sizes are the same; it was intended as an algebraic truth, not as code.

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!