What could be possible issue with colon operator here ?

Hello All . I am new to Matlab and I was trying to scale the allMarks Columns 1 to 3 by a factor of 10 and allMarks Column 4 by a factor of 5 and over write the allMarks matrix with the new values obtained by using colon operator (:) in between . But it is not going as expected . What could be the reason ?

 Respuesta aceptada

The colon operator in between the call to columns is incorrect. Remove it.
allMarks = [24 44 36 18;
52 57 68 38;
66 53 69 36.5;
85 40 86 36;
15 47 25 14
79 72 82 45.5];
%Corrected code
Scaled_marks = [allMarks(:,1:3)./10 allMarks(:,4)./5]
Scaled_marks = 6×4
2.4000 4.4000 3.6000 3.6000 5.2000 5.7000 6.8000 7.6000 6.6000 5.3000 6.9000 7.3000 8.5000 4.0000 8.6000 7.2000 1.5000 4.7000 2.5000 2.8000 7.9000 7.2000 8.2000 9.1000

3 comentarios

Siddhesh
Siddhesh el 6 de Jul. de 2023
Editada: Siddhesh el 6 de Jul. de 2023
Thank you for the help . On the hindsight , what is the colon doing ? For eg , when we say a = [1:5] it prints a = [ 1 , 2 , 3 , 4 , 5 ] . So it means it is dividing the first three columns by 10 and the last column by 5 and printing the new allMarks . Taking first row and first column element i.e. 2.4 , incrementing it by 1 and printing 3.4 . Again it is incrementing 3.4 by 1 i.e. 4.4 and after it finds 4.4 to be greater than 3.6 ( first element , first row of the matrix divided by 5 , it stops printing ? I guess this is what is happening ?
Stephen23
Stephen23 el 6 de Jul. de 2023
Editada: Stephen23 el 6 de Jul. de 2023
"what is the colon doing ?"
The colon operator is used for generating vectors and for indexing. Your example uses both of those:
"For eg , when we say a = [1:5] it prints a = [ 1 , 2 , 3 , 4 , 5 ]"
The colon generates a vector. Square brackets are a concatenation operator... but you are not concatenating that vector with anything, so why are you using superfluous square brackets? Get rid of them:
a = 1:5; % no superfluous square brackets.
"So it means it is dividing the first three columns by 10 and the last column by 5 and printing the new allMarks"
No, it divides the 4th column by 5, not the last column. In general these are not the same thing but they might be for your example matrix.
X = [allMarks(:,1:3)./10 allMarks(:,4)./5]
% ^ ^ all rows
% ^^^ column index array [1,2,3]
% ^ column index array 4
% ^ ^ ^ ^ subscript indexing
% ^ ^ concatenate some arrays together
Each of those indexing operations returns a matrix with the same number of rows, which are concatenated together horizontally. When you want to understand MATLAB code break it down into the smallest parts.
You would benefit from doing the introductory tutorials, which explain basic indexing and concatenation:
Thank you for the help Sir .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2023a

Preguntada:

el 5 de Jul. de 2023

Editada:

el 6 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by