Plotting MATLAB data from arrays with several dimensions - arrays

Consider the three-dimensional arrays
A = rand(3,4,5);
B = rand(3,4,5);
plot(A(:,1,1),B(:,1,1))
plot(A(1,:,1),B(1,:,1))
This all works fine, however
>> plot(A(1,1,:),B(1,1,:))
Error using plot
Data may not have more than 2 dimension
Is there a quick way around this a way around this other than using reshape()?

you should use squeeze to remove singleton dimensions:
plot(squeeze(A(1,1,:)),squeeze(B(1,1,:)))
another option is to shift matrix dimensions using shiftdim
plot(shiftdim(A(1,1,:),1),shiftdim(B(1,1,:),1),'o')

Related

Using N-D interpolation with a generic rank?

I'm looking for an elegant way of useing ndgrid and interpn in a more "general" way - basically for any given size of input and not treat each rank in a separate case.
Given an N-D source data with matching N-D mesh given in a cell-array of 1D vectors for each coordinate Mesh={[x1]; [x2]; ...; [xn]} and the query/output coordinates given in the same way (QueryMesh), how do I generate the ndgrid matrices and use them in the interpn without setting a case for each dimension?
Also, if there is a better way the define the mesh - I am more than willing to change.
Here's a pretty obvious, conceptual (and NOT WORKING) schematic of what I want to get, if it wasn't clear
Mesh={linspace(0,1,10); linspace(0,4,20); ... linsapce(0,10,15)};
QueryMesh={linspace(0,1,20); linspace(0,4,40); ... linsapce(0,10,30)};
Data=... (whatever)
NewData=InterpolateGeneric(Mesh,QueryMesh,Data);
function NewData=InterpolateGeneric(Mesh,QueryMesh,Data)
InGrid=ndgrid(Mesh{:});
OutGrid=ndgrid(QueryMesh{:});
NewData=interpn(InGrid{:},Data,OutGrid{:},'linear',0.0)
end
I think what you are looking for is how to get multiple outputs from this line:
OutGrid = ndgrid(QueryMesh{:});
Since ndgrid produces as many output arrays as input arrays it receives, you can create an empty cell array in this way:
OutGrid = cell(size(QueryMesh));
Next, prove each of the elements of OutGrid as an output argument:
[OutGrid{:}] = ndgrid(QueryMesh{:});

Using jsonencode with length 1 array

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:
jsonencode(struct('words', [string('hello'), string('bye')]))
Then this produces:
{"words":["hello","bye"]}
which is correct. If however I do:
jsonencode(struct('words', [string('hello')]))
Then it produces:
{"words":"hello"}
losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.
Any idea how I can work around this issue?
It seems this can be solved by using a cell rather than an array and then not creating the struct inline. Like
s.words = {'hello'};
jsonencode(s)
Output:
{"words":["hello"]}
I presume when created inline the cell functionality of matlab is actually trying to make multiple structs rather than multiple strings. Note that this still won't work with arrays as matlab treats a size one array as a scalar.

How to create single image from cell array of matrices in Matlab?

I needed to split a grayscale image in equal parts so I used the function mat2cell. Then I had to equalize each of the parts separatelly, for this purpose I used the function histeq. I reused the same cell array variable for this. Here is the code:
height=round(size(img,1)/number_of_divisions);
length=round(size(img,2)/number_of_divisions);
M=zeros(number_of_divisions,1);
N=zeros(1,number_of_divisions);
M(1:number_of_divisions)=height;
N(1:number_of_divisions)=length;
aux=mat2cell(img,M,N);
for i=1:size(aux,1)
for j=1:size(aux,2)
aux{i,j}=histeq(aux{i,j},256);
end
end
So now how do I merge each cell into one single image?
Use cell2mat
img2=cell2mat(aux);
For a better performance, replace your code with blockproc
blocksize=ceil(size(img)./number_of_divisions);
img2=blockproc(img,blocksize,#(block_struct)histeq(block_struct.data));

ActionScript 3 - 2D array for a chessboard

I'm designing a chess game in ActionScript 3 using Flash Professional CC. I've created a chessboard using the IDE and placed pieces in their initial positions. Each tile has its own instance and is named its respective coordinate e.g. the top left tile is called A8.
For calculating moves that are valid and such, I planned to use two 2D arrays of objects. One array should contain the tile instances e.g. A8, B8, C8, D8 etc. and the other should contain the pieces of the board e.g. BR1, BB1.
I've noticed that ActionScript does not allow one to implement 2D arrays like C++ (a language I'm familiar with); instead, nested arrays are used. I'm slightly confused about how to set up these arrays. What is the most efficient way to declare and initialise these arrays (hopefully not involving repetitive code)?
welcome to the army of AS3 developers.
Here's few tips for you:
Arrays can be defined as var array:Array = []; and here's a 2d Array - var a:Array = [[]];. Arrays are dynamic object, you don't need to specify the depth of the array. So when adding tile to the array just add them via array[x][y] = tile
Having said that, don't use arrays :) You strictly typed collectors called Vectors. You need to define Vectors: 1d var myVector:Vector.< Tile > = new Vector.< Tile >(); and 2d version var myVector:Vector< Vector.< Tile > >; and so on.
Vectors are faster than Arrays.
More tips:
Saving black and write tiles in Library and constructing the grid on run-time might be better for you.
Accessing object on screen by their instance names is a bad idea - a lot of work and poor maintainability. Plus you won't be able to access them before the scene is finished building and you'll access them as Dynamic objects, you'll need to cast them to your needed class to work with them normally. That also leads to silent bugs since you won't get warning on compile time if you try to access something illegal.

Storing realcolor images in an array for MATLAB

I am working on an m file that would take out single frames from a bigger image and play them as an animation. So far I managed to create the algorithm to locate and crop individual frames.
I can also store them in cell arrays. Almost everything is already done really.
My problem is that I can't get them to animate. I used the animation functions but they do not work. The reason being is that they are in cell arrays instead of just 4D arrays.
I want to store each frame in a nXmX3X(frame_number) array. How can I do that? How can I replace only the nXm part of an array?
Thank you.
if you have a cell array cFrames with n cells each storing the k-th frame of size m-by-n-by-3, you can use cat to create the desired 4D array
>> frames4d = cat(4, cFrames{:} );
Note: all frames in cFrames must have the same size for this to work.

Resources