How to compute matrix variable in mat file?

I want to compute variables in my matrix with this formula:
P = load('grading/dimension.mat','Z');
min_P = min(P.Z);
max_P = max(P.Z);
P.Z = ((0.8*(P.Z - min_P))/(max_P - min_P)) + 0.1;
but i got error message:
??? Error while evaluating uicontrol Callback
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> guikedelaizulfa>identifikasi_Callback at 1429
P.Z = ((0.8*(P.Z - min_P))/(max_P - min_P)) + 0.1;
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> guikedelaizulfa at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)guikedelaizulfa('identifikasi_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
WHat should I do? Can you help me?

 Accepted Answer

You Z variable is not a vector, so the max and min are not scalars. You are trying to subtract the non-scalars from the full matrix.
By default max() and min() work along the first dimension that is not of length 1; for non-vectors that is typically the first dimension, converting an M x N array into a 1 x N result. That might be what you want; if so then it would be better to explicitly specify the dimension number so that everyone immediately knows what is going on.
Are you wanting to subtract the minimum of each column from the respective column, scaling each column individually?
If what you are trying to do is scale the entire matrix according to the max and min over the entire matrix, then what you should do is
min_P = min(P.Z(:));
max_P = max(P.Z(:));
those will each be scalars, after which the rest of the code will work (unless, that is, the maximum and minimum values are equal, which we as on-lookers must point out that you have not ruled out in your problem definition.)

3 Comments

It does work, but looks like it saved min and max value of entire matrix . I need min and max value from each row,
nR = size(P.Z,2);
min_P = min(P.Z, [], 2);
max_P = max(P.Z, [], 2);
P.Z = 0.8 * (P.Z - repmat(min_P, 1, nR)) ./ repmat(max_P - min_P, 1, nR) + 0.1;
It works! Thank you very much for your suggestion ^^

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!