Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc;
- clear;
- more off;
- function [retval] = GetSuffix (number)
- ordinalIndicators = cellstr({"st"; "nd"; "rd"; "th"});
- numAsString = num2str(number);
- last2Digits = mod(number, 100);
- if(last2Digits >= 11 && last2Digits <= 13)
- suffix = ordinalIndicators{4};
- else
- modulo = mod(number, 10);
- if(modulo >= 1 && modulo <=3)
- suffix = ordinalIndicators{modulo};
- else
- suffix = ordinalIndicators{4};
- endif
- endif
- retval = strcat(numAsString, suffix);
- endfunction
- a = rand(4, 8); # creates the 4x8 matrix with random numbers between 0 (inclusive) and 1 (exclusive)
- comparisonStrings = cellstr({" and is not bigger than 0.5."; " and is bigger than 0.5."});
- disp(a);
- # these 2 loops will go through the matrix
- [rowCount, columnCount] = size(a);
- #disp(rowCount);
- #disp(columnCount);
- for row = 1:1:rowCount
- for column = 1:1:columnCount
- currentRowString = GetSuffix(row); # get a string based on the given number, for example 1 becomes "1st" and 4 becomes "4th"
- currentColumnString = GetSuffix(column); # same as above
- currentValue = a(row, column); # the current value of the element we want to check
- stringValue = currentValue > 0.5; # the comparison of the value to 0.5
- 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
- # this is a gigantic method call which pretty much concatenates all the strings together to the print output you want
- # i haven't found a better/more elegant way to concatenate strings in octave but this will do the job
- # note here that "cstrcat" will not ignore whitespaces for formatting while "strcat" does
- stringResult = cstrcat("The value in the ", currentRowString, " row and the ", currentColumnString, " column is ", num2str(currentValue), comparisonStrings{stringValue});
- disp(stringResult); # prints out the string for each value in the matrix
- endfor
- endfor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement