## How to multiply 2 columns by a scalar number in MATLAB

on 19 Sep 2013

### Jan (view profile)

Dear All
Hope you all are keeping good.i need 4 columns [X,Y,Z,Volume]in excel and first of all i want to multiply X and Y columns by 0.09 and (Z column contains just one scalar (0.18) value) then want to multiply all these X,Y,Z coordinates to get Volume. the code i am trying is given below. any help is appreciated.
if true
[x, y] = find(handles.maskedImage ~= 0);
% here x and y are created in excel by code line.to calculate the volume. first multiply 0.09 by both columns to get X and Y.
X=times(0.09,x);
Y=times(0.09,y);
Z = repmat(0.18,[length(x),1]);
[FileName,PathName] = uiputfile('*.xls','Save data to spreadsheet');
Volume=times(X,Y,Z);
Data=[X,Y,Z, Volume];
xlswrite([PathName '/' FileName],Data); end

Jan

### Jan (view profile)

on 19 Sep 2013
If you post code, explain if it is working well or if you have any problems with it.

on 19 Sep 2013
yes, it works thank you.
Image Analyst

### Image Analyst (view profile)

on 19 Sep 2013
No it doesn't. Think about what you are doing. Multiplying the x,y coordinates of every pixel in the selected region by the spatial calibration factor does not calculate the volume.

### Tags

No tags entered yet.

### Jan (view profile)

on 19 Sep 2013

X = 0.09 * x;
Y = 0.09 * y;
Volume = X .* Y * 0.18;

Image Analyst

### Image Analyst (view profile)

on 19 Sep 2013
That's what he asked for and that's what you gave and that's what he accepted, but Jan and I know that is not what he needs. Calculating volume this way is totally bogus. Not even close to being correct.
Jan

### Jan (view profile)

on 20 Sep 2013
@Image Analyst: I'm physicist and therefore very free in the choice of the definition of the term "volume", "pixel" and "coordinates". It could be a sound volume for a generalization of acoustic sound waves in a high-dimensional space with a linear metric. Or the coordinates could have been transformed before by a difference of neighboring elements.
But without doubt you are right and for the standard definition of a spatial volume in the Euclidean 3D space this calculations are not correct.
@Muhammad: You have to check the results with known test data, e.g. a line (Volume==0), a sphere and a rotated cube. I focused on avoiding the failing times().