Solving linear equation by matrix method in Octave - octave

I was writing a code to solve linear equations by matrix method. This is my code
A=input('Enter the coeffecient matrix A: ');
B=input('Enter the constant matrix B (column matrix form): ');
A
B
X=['x'; 'y'; 'z'];
R=inv(A)*B;
disp('Result');
R
This is what I did. But I want to get Output result to displayed in the form of
Matrix(X)=Matrix(R) so that people could directly compare the values of variables in Matrix (X) with Matrix (R) . It may very easy one but however I tried I am not getting it. How could I implement this ?

How about
prettyprint(R,["x";"y";"z"]," ")
Or you can add a column header instead of a blank character.
This function requires the econometrics package, so if you don't have that, first do
pkg install -forge econometrics
pkg load econometrics

I actually solved it by adding the code
for i=[1:1:3];
printf('%s = %f \n',X(i,:),R(i,:))
end

Related

problem when trying to load data with textscan

I want to load a set of data that contains variables type string and float. But when I use textscan in octave, my data doesn't load. I got matrix 1x6 (i have 6 features), but in this matrix I got cells that contains nothing(cells that are 0x1).
my code:
filename='data1.txt';
fileID = fopen(filename,'r');
data = textscan(fileID,'%f %s %s %f %f %s','Delimiter',',');
fclose(fileID);
when I for example try data(1):
>> data(1)
ans =
{
[1,1] = [](0x1)
}
>>
there is it
there is my set
Also my file id isn't -1.
I had been searching for in the ethernet problem like this but I couldn't find any.
I tried to delete headers in data and smaller training set but it don't work.
Pls help.
Don't use textscan. Textscan is horrible, and one should only use it when they're trying to parse data when there's no better way available.
Your data is a standard csv file. Just use csv2cell from the io package.

How to replace MATLAB's timeseries and synchronize functions in Octave?

I have a MATLAB script that I would like to run in Octave. But it turns out that the timeseries and synchronize functions from MATLAB are not yet implemented in Octave. So my question is if there is a way to express or replace these functions in Octave.
For understanding, I have two text files with different row lengths, which I want to synchronize into one text file with the same row length over time. The content of the text files is:
Text file 1:
1st column contains the distance
2nd column contains the time
Text file 2:
1st column contains the angle
2nd column contains the time
Here is the part of my code that I use in MATLAB to synchronize the files.
ts1 = timeseries(distance,timed);
ts2 = timeseries(angle,timea);
[ts1 ts2] = synchronize(ts1,ts2,'union');
distance = ts1.Data;
angle = ts2.Data;
Thanks in advance for your help.
edit:
Here are some example files.
input distance
input roation angle
output
The synchronize function seems to create a common timeseries from two separate ones (here, specifically via their union), and then use interpolation (here 'linear') to find interpolated values for both distance and angle at the common timepoints.
An example of how to achieve this to get the same output in octave as your provided output file is as follows.
Note: I had to preprocess your input files first to replace 'decimal commas' with dots, and then 'tabs' with commas, to make them valid csv files.
Distance_t = csvread('input_distance.txt', 1, 0); % skip header row
Rotation_t = csvread('input_rotation_angle.txt', 1, 0); % skip header row
Common_t = union( Distance_t(:,2), Rotation_t(:,2) );
InterpolatedDistance = interp1( Distance_t(:,2), Distance_t(:,1), Common_t );
InterpolatedRotation = interp1( Rotation_t(:,2), Rotation_t(:,1), Common_t );
Output = [ InterpolatedRotation, InterpolatedDistance ];
Output = sortrows( Output, -1 ); % sort according to column 1, in descending order
Output = Output(~isna(Output(:,2)), :); % remove NA entries
(Note, The step involving removal of NA entries was necessary because we did not specify we wanted extrapolation during the interpolation step, and some of the resulting distance values would be outside the original timerange, which octave labels as NA).

Generation of plot legend names in GNU Octave

I would like to generate the legends of a plot in GNU Octave, based on how many lines I plot, how do I do this?
The names of each legend should be the same except a number in the name.
Here is what i do now:
W = 3
data = zeros(W, 1000);
% Calculate data...
plot(data.');
legend("w1", "w2", "w3");
The problem arises when I change W to some other value, then i have to manually update the legend call with more or less strings.
for i=1:W
leg(i,:)=strjoin({"W",int2str(i)},"");
endfor
legend(leg)
You can specify the name of the line in the plot command, through the DisplayName line property. legend will use these names:
clf; hold on
plot(data(1,:), 'DisplayName','w1');
plot(data(2,:), 'DisplayName','w2');
legend;

Prevent Jupyter from printing loaded matrices with the octave kernel

I'm using the Octave kernel in a Jupyter notebook. When I try to load a matrix from a file using the dlmread function, the whole data is printed. I tried assigning the result to a variable immediately, but no luck. This is troublesome because the matrix is quite large (~17*500) so it takes a while to print.
I just want to load my data without having all of it printed.
I guess you don't have a ; at the end of your dlmread command. See the manual:
Ending a command with a semicolon tells Octave not to print the result of the command
See this example:
>> a = 4 + 5
a = 9
>> b = 5 + 6;
>>

Matrix generation with octave in a loop

Hi I want to generate some random matrices of size Nx3 for varying N, in GNU-Octave. I want to save each of these matrices to a different file.
The script below almost does the job, but strangely it produces only file with the name int2str(N) ; it keeps overwriting the files produced at the previous iteration.
for i=1:10
N=(2**i)
A=rand(N,3);
save int2str(N) A
end
The script is interpreting int2str(N) itself as a string. How do I avoid this behaviour?
You can invoke save with the function form:
save(int2str(N), "A")