Advertisement
Guest User

Untitled

a guest
Aug 4th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.97 KB | None | 0 0
  1. clc;
  2. clear;
  3. more off;
  4.  
  5. function [retval] = GetSuffix (number)
  6.   ordinalIndicators = cellstr({"st"; "nd"; "rd"; "th"});
  7.   numAsString = num2str(number);
  8.  
  9.   last2Digits = mod(number, 100);
  10.   if(last2Digits >= 11 && last2Digits <= 13)
  11.     suffix = ordinalIndicators{4};
  12.     else
  13.      modulo = mod(number, 10);
  14.    if(modulo >= 1 && modulo <=3)
  15.       suffix = ordinalIndicators{modulo};
  16.     else
  17.       suffix = ordinalIndicators{4};
  18.     endif
  19.   endif
  20.   retval = strcat(numAsString, suffix);
  21. endfunction
  22.  
  23.  
  24. a = rand(4, 8); # creates the 4x8 matrix with random numbers between 0 (inclusive) and 1 (exclusive)
  25.  
  26. comparisonStrings = cellstr({" and is not bigger than 0.5."; " and is bigger than 0.5."});
  27.  
  28. disp(a);
  29.  
  30. # these 2 loops will go through the matrix
  31.  
  32. [rowCount, columnCount] = size(a);
  33.  
  34. #disp(rowCount);
  35. #disp(columnCount);
  36.  
  37. for row = 1:1:rowCount
  38.   for column = 1:1:columnCount
  39.     currentRowString = GetSuffix(row); # get a string based on the given number, for example 1 becomes "1st" and 4 becomes "4th"
  40.     currentColumnString = GetSuffix(column); # same as above
  41.    
  42.     currentValue = a(row, column); # the current value of the element we want to check
  43.     stringValue = currentValue > 0.5; # the comparison of the value to 0.5
  44.     stringValue++; # we add 1 here because octave arrays start at 1 and not at 0 so we have to add 1 for accessing the strings
  45.    
  46.     # this is a gigantic method call which pretty much concatenates all the strings together to the print output you want
  47.     # i haven't found a better/more elegant way to concatenate strings in octave but this will do the job
  48.     # note here that "cstrcat" will not ignore whitespaces for formatting while "strcat" does
  49.     stringResult = cstrcat("The value in the ", currentRowString, " row and the ", currentColumnString, " column is ", num2str(currentValue), comparisonStrings{stringValue});
  50.     disp(stringResult); # prints out the string for each value in the matrix
  51.    
  52.   endfor
  53. endfor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement